Detail
#28935
TL-Script: Function as the result of an attribute in the model cannot access variables from its context
It is possible to return a function with the storage rule "Calculation via TL script", which is then fed with parameters, e.g. to calculate a value.
It should be noted that "Unspecified" is selected as the data type, and not "Search expression" (unsightly, but not critical).
A problem arises if the returned function in turn refers to variables that were defined outside. An error appears: "Access to undefined variable .... .". If the purpose is to perform complex calculations once and only continue with the result, this is therefore not possible.
== Example A project knows the cost classification "high" or "low". A cost rate can provide values for "high" or "low". The project should provide a calculation function that receives a cost rate as a parameter should then provide either the value of "high" or "low" according to the project.
// Variable in context of result function classification = $project.get(`projectStructure:Project#costClassification`); // Function computing a value based on the variable computed before. cost-> switch { "high".equals($classification): $cost.get(`exampleModule:Kostensatz#valueHigh`); "low".equals($classification): $cost.get(`exampleModule:Kostensatz#valueLow`); default: 0; }
The function cost->..., which is returned as a result, cannot be applied to a cost rate because when the function is called outside the block from which it is returned, the $classification variable can no longer be accessed from its original context.
This is just an example that can be easily modified, but in general you want to be able to access variables from the scope of a function.
Test
Technically:
{ increments = count(5).map(x -> y -> $y + $x); count(5).map(n -> $increments[$n](0)); }
The expected result is [0, 1, 2, 3, 4]. Currently, [4, 4, 4, 4, 4] is incorrectly returned because the increment function does not remember its context.