|
|
|
|
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879428 is a reply to message #879287] |
Thu, 31 May 2012 07:57 |
|
What Xtend version are you guys using?
In the latest Xtext version 2.3.0.v201205301420 I have :
«FOR i: 1..0»
«println("Loops twice loop "+i)»
«ENDFOR»
Loops twice. @Henrik - as expected
... and
«FOR i: Integer::MIN_VALUE..0»
«println("Crappy Loop "+i)»
«ENDFOR»
doesn't loop at all, because of an IllegalArgumantException in org.eclipse.xtext.xbase.lib.IntegerRange
+Dennis Huebner
Get professional support from the Xtext committers at www.typefox.io
[Updated on: Thu, 31 May 2012 08:01] Report message to a moderator
|
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #879495 is a reply to message #879287] |
Thu, 31 May 2012 10:07 |
Ed Willink Messages: 7679 Registered: July 2009 |
Senior Member |
|
|
Hi
The equivalent x..y construct is poorly specified in OCL; it actually
involves an upward search from x to y that never terminates since OCL's
numbers are unlimited.
I was proposing to generalize as: x to y inclusive, in the direction of
y, but found that this had some bad properties when y is a computed
result such as size() that ends up smaller than expected.
If X**** still has the discretion to define this, I recommend an empty
iteration for y < x. The iteration is x up to y, so long as x <= y.
Regards
Ed Willink
On 31/05/2012 00:13, Henrik Lindberg wrote:
> On 2012-30-05 16:18, Christian Dietrich wrote:
>> Hi,
>>
>> it does loop. in fac very often
>>
>> 1..0 doesnt loop
> That looks like a bug - I would expect to iterations (for '1' and for
> '0').
>
> Why not just use a function exclusive(T, T) and/or variant taking
> single range as argument, where exclusive returns range that is "one
> less" or an empty iterator if range is empty.
>
> for(x : exclusive(0..0))
> for(x : exclusive(0, 0))
>
> - henrik
|
|
|
|
|
Re: How do I create a FOR loop that runs once? [message #880016 is a reply to message #879611] |
Fri, 01 June 2012 09:31 |
Aaron Digulla Messages: 258 Registered: July 2009 Location: Switzerland |
Senior Member |
|
|
That design choice isn't as arbitrary as it might seem because it has consequences. Let's look at the Java String API. We want to get the sub string before the first comma:
int pos = s.indexOf( ',' )
String result = s.substring( 0, pos )
String rest = s.substring( pos )
String restWithoutComma = s.substring( pos + 1 )
If the range parameter for substring() was upper-inclusive, I'd have to write "s.substring( 0, pos-1 )".
Someone might want to fix this by returning a 1-based index from indexOf() (i.e. 0 would mean "not found" and 6 for indexOf( "Hello,", ',' ) instead of 5). That would give this code:
int pos = s.indexOf( ',' )
String result = s.substring( 0, pos )
String rest = s.substring( pos - 1 )
String restWithoutComma = s.substring( pos )
unless we make the start-index 1-based too which would make it incompatible with arrays. If someone wants to fix that: Why do you write code in Java when you'd be better off with Pascal? And don't come complaining when you realize how many 1-off errors you start to make all of a sudden.
This would also influence all such APIs like Regexp matching and range loops.
To get the first four characters would be "substring( 0, 3 )" instead of "substring( 0, 4 )".
If you still have doubts that this is a bad thing, have a look at the JDBC API which uses 1-based indices for columns. I got my "loop over all columns" loops so often wrong that I've wrote my own wrapper library to get proper 0-based indices.
Conclusion: Trying to solve this design problem by applying English language rules might seem natural but it tries to hide the underlying hardware model with an either inconsistent or clumsy design that causes problems as soon as you try to start to use it.
Proof: The Xtend developers, who seem to be pretty smart, got IntIterator wrong in Xbase 2.2.x.
|
|
|
Powered by
FUDForum. Page generated in 0.04313 seconds