Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

new simple for loop syntax
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Tue Dec 01, 2009 10:34 pm    Post subject: new simple for loop syntax Reply with quote

Continuing from Smarty 3.0 Alpha 1: Proof of Concept thread.

mohrt wrote:
douglassdavis wrote:
PHP/C style for loops are usually one of the most difficult things for non-programmers and novice programmers to learn. They often run into infinite loops and other problems.

I'm sure it's too late to mention this now, and it may be a totally stupid, but, since I just thought about it, I'll bring up the question anyway. Laughing What if the for loops were more Basic style?

I.e. simple syntax, no infinite loops.


Code:

{for $x = 0 to 5}
  HTML here
{/for}


Code:

{for $x = 5 to 0 step -1}
  HTML here
{/for}


Great idea. Lets flesh this out. The new {foreach} syntax and this {for} syntax will be a welcome addition. The traditional for and foreach syntax will also be supported, but the simpler syntaxes will be outlined in the docs.

It does not forgo infinite loops, but it makes an easier read.


Great! C/PHP for loops allow people to create some weird/complicated syntax and can have many unintended consequences. So, the simple version would be good, especially for a templating language.


I think that would be a good idea to eliminate infinite loops when possible. Example, don't iterate at all when:

start > end and step is positive
start < end and step is negative

(step by default is 1)


I suppose totally eliminating infinite loops would be difficult or impossible because it would be impossible to prevent some one from assigning to the counter variable

Also, it would be difficult/impossible to prevent some one from creating a nested loop that uses the same counter variable, although, the compiler probably could do it theoretically.


Just throwing out another idea.. It might be unprecedented as far as other languages go, but you could keep track of the value of the counter variable in a "shadow" variable associated with that specific for loop, and always iterate/assign to the counter variable from that shadow variable.


With that setup, even this wouldn't be a problem:
Code:

  {for $x = 1 to 2 }
     {for $x = 1 to 3 }
        {$x}
        {assign var=x value ='foo'}
        {$x}
     {/for}
   {/for}


It would print out
1 foo 2 foo 3 foo 1 foo 2 foo 3 foo

that would make infinite loops impossible and prevent users from messing things up. downside being it would prevent manipulating the counter to do things like skipping one iteration.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 02, 2009 2:07 am    Post subject: Reply with quote

We likely won't try to address the issue of clobbering the {for} variable, you should not nest the same var or assign other values from within the loop, just as you would expect.

It would make sense to have special for loop variables, much the same as we have special vars for {section} and {foreach} loops. You could always get the current iteration, first, last, etc.

As for default step value, it could be 1 or -1 depending on the direction of the for loop.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Dec 02, 2009 3:09 am    Post subject: Reply with quote

mohrt wrote:
We likely won't try to address the issue of clobbering the {for} variable, you should not nest the same var or assign other values from within the loop, just as you would expect.

It would make sense to have special for loop variables, much the same as we have special vars for {section} and {foreach} loops. You could always get the current iteration, first, last, etc.

As for default step value, it could be 1 or -1 depending on the direction of the for loop.


ok on the issue of clobbering the variable, and the standard first, last iteration sound good.

I think the default step value is always 1 in VB for a good reason though. I've found that feature combined with the feature that it doesn't iterate at all when start>end and step>0 to be very useful.

This is especially true when using variables for start and end values.

For example, when end represents an absolute end of some sort, and start represents some calculated or requested value. Or vice-versa. It works as an automatic out-of-bounds checker.

Plus I think in general the output of for loops is either meant to count one way or the other. Rarely is it meant to switch directions automatically. I suppose there could be a "Step auto" option if it's important to have though.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 02, 2009 4:18 am    Post subject: Reply with quote

Makes sense. So default step is always 1 and if start >= end, it doesn't loop.
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Dec 02, 2009 5:52 am    Post subject: Reply with quote

mohrt wrote:
Makes sense. So default step is always 1 and if start >= end, it doesn't loop.


don't loop if start>end and step is positive, don't loop if start<end and step is negative.

is equivalent to

for ($x=$start;$x<=$end;$x+=$step)

or

for ($x=$start;$x>=$end;$x-=$step)





another thing, in cases like

Code:

{for $x is $start to $end Step $step}


Although $x can be manipulated in the loop and affect the loop iterations, I think it is important that even when $start, $end, and $step are manipulated in the loop, it should not affect the loop iterations. I would think that those values would have to be copied when the loop starts, so it wouldn't be a problem. But I just wanted to mention it as an important implementation detail.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 02, 2009 7:09 pm    Post subject: Reply with quote

Detail question: should the syntax be =, is or from?

Code:
{for $x = $start to $end step $step}


Code:
{for $x is $start to $end step $step}


Code:
{for $x from $start to $end step $step}


I'm partial to "from", although we could support "=" as well. I don't think "is" fits here.

This syntax is different enough, a new func name might help:

Code:
{loop $x from 0 to 10}...{/loop}
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Wed Dec 02, 2009 8:07 pm    Post subject: Reply with quote

"loop" to me seems to be a more generic term. With for, you immediately know you are counting from one number to another. Although, you do have a point that it doesn't look like a PHP for (if that's what you mean by different), I think people might easily get used to the idea that Smarty != PHP, and will come to appreciate the new syntax. Maybe a different word like "count" or "iterate" which might make "from" seem more appropriate.

Maybe even change "step" to "by."

Code:
{count $x from $start to $end by $step}


I'm partial to =, but, I'm basing my concept off of VB and I think more other languages use = for a for loop than "from" (including PHP).

Also, to my eyes, these words run together a little more:

Code:
{for $x from $start to $end step $step}


Than this:

Code:
{for $x = $start to $end step $step}



I'm not really adamant about either though. The important aspect to me is it's simple and less error prone than the C/PHP version.


Any body else have an opinion?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Wed Dec 02, 2009 10:18 pm    Post subject: Reply with quote

I would keep "step", as there is no real advantage to change that to "by", but only add confusion.

On further thought about "loop" again, just more syntax confusion. We can stick with "for".

I think I like the "=", it makes better sense:

Code:
{for $x = 1 to 5} ... {/for}


The = implies that $x is assigned the iterative value on each loop better than "from" does.
Back to top
View user's profile Send private message Visit poster's website
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Thu Dec 03, 2009 10:44 pm    Post subject: Reply with quote

{for $x = $start to $end step $step} ... {/for}is in the SVN now .

You can use also
{for $x = $start to $end} ... {/for}

In this case the step value will be automaticall 1 or -1 depending on the start and end values.

Instead of $start and $end you can use any valid expression.

Inside the loop the following special vars can be accessed:

$x@iteration = number of iteration
$x@total = total number of iterations
$x@first = true on first iteration
$x@last = true on last iteration
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 12:45 am    Post subject: Reply with quote

U.Tews wrote:
{for $x = $start to $end step $step} ... {/for}is in the SVN now .

You can use also
{for $x = $start to $end} ... {/for}

In this case the step value will be automaticall 1 or -1 depending on the start and end values.


Instead of $start and $end you can use any valid expression.

Inside the loop the following special vars can be accessed:

$x@iteration = number of iteration
$x@total = total number of iterations
$x@first = true on first iteration
$x@last = true on last iteration


Good work. Thanks.

I think we decided against the bolded part.

IMO it would cause more problems than it solves.
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Dec 04, 2009 1:00 am    Post subject: Reply with quote

I forgot to mention that the step direction is monitored. When it is wrong you can enter a {forelse} section.

Example
{for $x=0 to 5 step -1}....{forelse}wrong direction{/for}
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 1:04 am    Post subject: Reply with quote

What would you expect this to do?

Code:
{for $x = 5 to 1}


* do nothing (default step of 1)
* loop from 5 down to 1 (default step of -1)

Which do you think?
Back to top
View user's profile Send private message Visit poster's website
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Fri Dec 04, 2009 1:05 am    Post subject: Reply with quote

mohrt wrote:
What would you expect this to do?

Code:
{for $x = 5 to 1}


* do nothing (default step of 1)
* loop from 5 down to 1 (default step of -1)

Which do you think?


good question.

if it was literals, just looking at it, i would expect it to count downwards.

if it was variables, looking at it, who knows what to expect. could count either way but it depends on how the loop is implemented
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Fri Dec 04, 2009 1:23 am    Post subject: Reply with quote

I think the step direction could be determined automatically. If you want to protect yourself against unwanted step directions you can always do so by specifying it explicitly.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 04, 2009 1:34 am    Post subject: Reply with quote

How about this one, what to expect?

Code:
{for $x = 100 to 1 step 10}


* Nothing? (step is positive 10)
* count from 100 to 1 by 10? (step is negated, as a positive makes no sense)

How about this english-style syntax:

Code:
{loop $x from 100 to 1 by 10}


In that case there is little doubt what the intention was.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Smarty 3 All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP