|
|
Re: Xtend2 - sorting by a function [message #668038 is a reply to message #668036] |
Tue, 03 May 2011 13:36 |
Sebastian Zarnekow Messages: 3118 Registered: July 2009 |
Senior Member |
|
|
Hi Sven, hi Andrew,
since it's well documented (since M7) which functions modify the passed
list and which create new copies of the input, I'm inclined to not
change the behavior of sort. Otherwise it's hardly possible to sort an
existing list in a convenient way with Xtend.
Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Am 03.05.11 15:28, schrieb Sven Efftinge:
> It's because (currently) the sort functions do modify the passed list.
> EMF puts certain constraints on the list which are validated during the
> sort operation. I think we should change that and return a new list
> instead and leave the old one unmodified.
> Please open a bugzilla.
>
> Thanks,
> Sven
>
>
> Am 5/3/11 2:09 PM, schrieb Andrew Clark:
>> Using the MyDsl example in Xtext 2, I have written the following Xtend
>>
>> class MyDslGenerator implements IGenerator {
>>
>> override void doGenerate(Resource resource, IFileSystemAccess fsa) {
>> for(model: resource.allContentsIterable.filter(typeof(Model))) {
>> fsa.generateFile("example.txt", model.hello)
>> }
>> }
>>
>> hello (Model m) {
>> ''' Longest is «m.longest»
>> '''
>> }
>>
>> longest(Model m) {
>> m.greetings.sortBy(e|e.length).last.name
>> }
>>
>> longest2(Model m) {
>> m.greetings.map(e|e.length).sort.last
>> }
>>
>> int length(Greeting g) {
>> g.name.length
>> }
>>
>> }
>>
>>
>> When this runs I get
>>
>>
>> Exception in thread "main" java.lang.IllegalArgumentException: The 'no
>> duplicates' constraint is violated
>> at
>> org.eclipse.emf.common.util.AbstractEList.set(AbstractEList. java:268)
>> at
>> org.eclipse.emf.common.util.AbstractEList$EListIterator.doSe t(AbstractEList.java:963)
>>
>> at
>> org.eclipse.emf.common.util.AbstractEList$EListIterator.set( AbstractEList.java:941)
>>
>> at java.util.Collections.sort(Unknown Source)
>> at
>> org.eclipse.xtext.xbase.lib.ListExtensions.sortBy(ListExtens ions.java:85)
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.longest(MyD slGenerator.java:46)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>>
>> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
>> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>>
>>
>>
>> alternatively I can try the longest2 function and get
>>
>>
>> Exception in thread "main" java.lang.UnsupportedOperationException
>> at java.util.AbstractList.set(Unknown Source)
>> at java.util.AbstractList$ListItr.set(Unknown Source)
>> at java.util.Collections.sort(Unknown Source)
>> at
>> org.eclipse.xtext.xbase.lib.ListExtensions.sort(ListExtensio ns.java:44)
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.longest2(My DslGenerator.java:61)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.hello(MyDsl Generator.java:32)
>>
>> at
>> org.xtext.example.mydsl.generator.MyDslGenerator.doGenerate( MyDslGenerator.java:24)
>>
>> at org.xtext.example.mydsl.generator.Main.runGenerator(Main.jav a:63)
>> at org.xtext.example.mydsl.generator.Main.main(Main.java:32)
>>
>>
>> Is this a bug (this worked in Xtend 1)?
>>
>
>
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05397 seconds