Replies: 1 comment
-
|
We now have I think this replaces I'm also curious about for item of list
if match item
break with item
unless break
'not found' // more likely, throw or return or somethingfor item of list
if match item
break with item
if break
console.log 'found', for.valueI also think we should consider the for item of list
if match item
break with item
else
'not found' // more likely, throw or return or somethingitem :=
for item of list
if match item
break with item
else
null // instead of getting an array of results
if item?
console.log 'found', for.valueNote: if we for item of list
if match item
console.log 'found', item
break
else
console.log 'not found' |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
for.valueFor loop expressions, let's expose the array where values get accumulated, so you can add extra items, remove items, reset the entire value, etc. This gives you pretty full control on adding items within the loop.
for.value,while.value,until.value,loop.value,do.valueto access the value from the nearest ancestor loop of the given typefor:label.valueand maybe:label.valueto get the value of a labeled loopfor:label item of listas more symmetric alternative for:label for item of listfor.pushforfor.value.pushresultsobject:resultsobject to something other than[]:=or.=)Open question: Would
if.valuebe useful to access the argument toif? Maybe not, as that seems pretty different (if's argument instead of return value).Postfix clause
We still would like control on the loop's value at the end of the loop. We could support a postfix clause that always runs at the end. I think
thenis a decent name. (It's liketry'sfinallyclause, butfinallyclauses get run not returned, sothenseems like a better name.)Admittedly, this example could also be written as
or more generally with pipe syntax:
So this feature may not be important. The next one is more interesting.
Postfix conditionals
It's common (e.g. in Python) to distinguish whether the loop
breaked or not. We propose a postfix conditional usingbreak?to enable distinguishing. More generally, allow assigning abreak.valueviabreak = ..., similar to #361.Perhaps more interesting when we're not accumulating a
resultsarray:I think we only want to a conditional after a loop as a postfix conditional when it uses
break?orbreak.value. Otherwise, we'd get surprising results where we build and even return theresultsarray but just expected a regularif/elseexpression:breaksetsbreak.valuetotruebreak = valuesetsbreak.valuetovalueif/unlesswhen they usebreak.valueorbreak?(shorthand forbreak.value?)(transcribing last night's conversation with @STRd6)
Beta Was this translation helpful? Give feedback.
All reactions