Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How do I create a FOR loop that runs once?
How do I create a FOR loop that runs once? [message #879054] Wed, 30 May 2012 12:50 Go to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
How do I create a FOR loop that runs once?

I tried:

«FOR i: 0..0» - doesn't loop at all
«FOR i: 0..1» - loops twice

How do I do this?

I'm using Xtext 2.2.1

[Updated on: Wed, 30 May 2012 13:37]

Report message to a moderator

Re: How do I create a FOR loop that runs once? [message #879061 is a reply to message #879054] Wed, 30 May 2012 13:00 Go to previous messageGo to next message
Dennis Huebner is currently offline Dennis HuebnerFriend
Messages: 257
Registered: July 2009
Senior Member

«FOR i: 0..0»
works for me with the latest Xtext version.



+Dennis Huebner

Get professional support from the Xtext committers at www.typefox.io

[Updated on: Wed, 30 May 2012 13:01]

Report message to a moderator

Re: How do I create a FOR loop that runs once? [message #879080 is a reply to message #879061] Wed, 30 May 2012 13:35 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
:-/ I fear that creates another hole: I can't create a loop that doesn't loop because «FOR i: 0..-1» would run from 0 to -1 (two iterations).

Can I override upTo() globally? I need inclusive ranges only once or twice per year.
Re: How do I create a FOR loop that runs once? [message #879083 is a reply to message #879080] Wed, 30 May 2012 13:38 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
You may want to follow https://bugs.eclipse.org/bugs/show_bug.cgi?id=371957

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 30.05.12 15:35, schrieb Aaron Digulla:
> :-/ I fear that creates another hole: I can't create a loop that doesn't
> loop because «FOR i: 0..-1» would run from 0 to -1 (two iterations).
>
> Can I override upTo() globally? I need inclusive ranges only once or
> twice per year.
>
Re: How do I create a FOR loop that runs once? [message #879097 is a reply to message #879080] Wed, 30 May 2012 14:06 Go to previous messageGo to next message
Dennis Huebner is currently offline Dennis HuebnerFriend
Messages: 257
Registered: July 2009
Senior Member

> :-/ I fear that creates another hole: I can't create a loop that doesn't
> loop because «FOR i: 0..-1» would run from 0 to -1 (two iterations).
>

So the topic should be "How do I create a FOR loop that doesn't loop?" Smile

P.S.: here is one that doesn't loop:
«FOR i: Integer::MIN_VALUE..0»

Razz



+Dennis Huebner

Get professional support from the Xtext committers at www.typefox.io
Re: How do I create a FOR loop that runs once? [message #879104 is a reply to message #879097] Wed, 30 May 2012 14:18 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

it does loop. in fac very often

1..0 doesnt loop


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I create a FOR loop that runs once? [message #879106 is a reply to message #879104] Wed, 30 May 2012 14:20 Go to previous messageGo to next message
Dennis Huebner is currently offline Dennis HuebnerFriend
Messages: 257
Registered: July 2009
Senior Member

Not for me...


+Dennis Huebner

Get professional support from the Xtext committers at www.typefox.io
Re: How do I create a FOR loop that runs once? [message #879287 is a reply to message #879104] Wed, 30 May 2012 23:13 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
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 #879428 is a reply to message #879287] Thu, 31 May 2012 07:57 Go to previous messageGo to next message
Dennis Huebner is currently offline Dennis HuebnerFriend
Messages: 257
Registered: July 2009
Senior Member

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 Razz



+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 #879431 is a reply to message #879428] Thu, 31 May 2012 08:00 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
I tested using 2.2.1
seems a lot has changes here.
crys for some docs in the migration guide


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How do I create a FOR loop that runs once? [message #879478 is a reply to message #879431] Thu, 31 May 2012 09:48 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
And more unit tests...
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 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
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 #879594 is a reply to message #879495] Thu, 31 May 2012 13:42 Go to previous messageGo to next message
Aaron Digulla is currently offline Aaron DigullaFriend
Messages: 258
Registered: July 2009
Location: Switzerland
Senior Member
Can you give an example when you could need an inclusive range?

My common use case is that I need an index while I iterate over a list. That index must go from 0 to size-1 (inclusive) or 0 <= index < size.
Re: How do I create a FOR loop that runs once? [message #879611 is a reply to message #879594] Thu, 31 May 2012 14:08 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

It's an almost arbitrary design choice.

In casual English, the ambiguity can be resolved as inclusive, and when
booking hotel rooms this can lead to trouble on the final day.

e.g. A decimal number is a number from 1 to 10; well actually 0 to 9
inclusive (in Java) or 1 to 10 inclusive (in OCL) or is it????

Inclusive has the almost irrelevant benefit that you can include
MAX_INTEGER in the range.

Regards

Ed Willink


On 31/05/2012 14:42, Aaron Digulla wrote:
> Can you give an example when you could need an inclusive range?
>
> My common use case is that I need an index while I iterate over a
> list. That index must go from 0 to size-1 (inclusive) or 0 <= index <
> size.
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 Go to previous message
Aaron Digulla is currently offline Aaron DigullaFriend
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.
Previous Topic:Xbase: overwrite the Operator '='
Next Topic:resource descriptions in unit tests
Goto Forum:
  


Current Time: Thu Apr 18 08:35:08 GMT 2024

Powered by FUDForum. Page generated in 0.01926 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top