Opened 10 years ago
Closed 10 years ago
#2811 closed enhancement (fixed)
Add support for list range expressions in for-loops
Reported by: | Per Östlund | Owned by: | Martin Sjölund |
---|---|---|---|
Priority: | normal | Milestone: | Bootstrapping |
Component: | MetaModelica | Version: | trunk |
Keywords: | Cc: |
Description
It would be nice if lists could be used as the range expression in for-loops in MetaModelica, to make it possible to write functions like this:
function splitOnTrue<T> input list<T> inList; input PredicateFunc inFunc; output list<T> outTrueList = {}; output list<T> outFalseList = {}; partial function PredicateFunc input T inElement; output Boolean outResult; end PredicateFunc; algorithm for e in inList loop // <- Iterate over list. if inFunc(e) then outTrueList := e :: outTrueList; else outFalseList := e :: outFalseList; end if; end for; outTrueList := listReverse(outTrueList); outFalseList := listReverse(outFalseList); end splitOnTrue;
Currently we only allow iteration over arrays, but making the frontend accept lists too is trivial. The generated code is specific for arrays though, so at the very least the code generator needs to be updated to handle lists. Maybe something needs to be done about polymorphic types too, I don't know how that's handled right now.
while-loops can be used as a workaround, but then you need to handle the iteration yourself which isn't quite as nice.
Change History (3)
comment:1 by , 10 years ago
Status: | new → accepted |
---|
comment:2 by , 10 years ago
comment:3 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | accepted → closed |
Testcase added as source:trunk/testsuite/metamodelica/meta/ForIterList.mos@22238
As long as the iterator type is returned as BOXED(INTEGER), it is fine. The variable will be unboxed in all the places it is used, which might be slightly inefficient but should be totally okay.