Tools
Sign in
Login
Forum
Dynamic binding
Forum
>
Scripting
>
Dynamic binding
n/A
posted
on April 3, 2014
at 4:53 PM
n/A
posted
on April 3, 2014
at 4:53 PM
Is there a way to bind dynamically a variable ? Something like $myObject.$var.
For example, $myObject.one, $myObject.foo, $myObject.bar where one,foo,bar is originally contained in a variable
Thanks
n/A
replied
on April 4, 2014
at 12:16 PM
n/A
replied
on April 4, 2014
at 12:16 PM
Hi Nicolas,
I'm not sure I understand your question, but if you are referring to "assigning the same pointer" it is possible if your property is a complex type.
For example:
$person is your object and it contains a property called "identification" of type Identification, then you can create a variable $identification and assign it like this:
$person.identification := $identification
This way the two variables will point to the same object, so if you change a property in the $identifcation object it will change the object referred to as $person.identification. If you change the whole $identication pointer however, by doing:
$identification := new Identification
Then the $person.identification object will be detached as it will still refer to the previously created object.
If you have primitive types only the latter case will apply as they are normally immutable (unless you use mutable Java objects and Java methods to update them).
If you are asking something completely different, like if you can have dynamic property names on a Data Class which are specified by another variable and not in the Data Class editor, I think you should consider using a Named Collection for your property so you can do:
$myObject[$var] := 'value';
and have an arbitrary number of possible properties. They need to be of the same parent type, so in the worst case scenario you might need to create a Named of type Any, thus making it possible to have different kinds of objects in it.
I'm not sure if you are referring to one of the two cases above, or if your use case is a third one. Can you please clarify?
n/A
replied
on April 4, 2014
at 3:01 PM
n/A
replied
on April 4, 2014
at 3:01 PM
Hi Mauro,
Thanks for the reply. Actually, I simply want to dynamically create a variable:
$app := 'way';
${$app} := 'hello appway';
PRINTLN ($way);
Thanks,
Nicolas
n/A
replied
on April 4, 2014
at 3:11 PM
n/A
replied
on April 4, 2014
at 3:11 PM
Hi Nicolas,
I'm not sure which use case are you trying to solve with this design. Our Script Language doesn't support those dynamic variable names natively as it will make it harder to validate the code and also to read it. We have an EVAL('string') function in the script language which has been used in the past to do something similar, but as I said whenever you use it the validation/dependencies won't work anymore and it is usually a bad design pattern.
If you tell me more about your use case I might help you finding an alternative solution.
n/A
replied
on April 4, 2014
at 3:30 PM
n/A
replied
on April 4, 2014
at 3:30 PM
Thanks for the quick reply.
This was more a general question, as we had a related discussion with a customer.
Nicolas
Thomas Suter
replied
on April 24, 2014
at 9:17 AM
Thomas Suter
replied
on April 24, 2014
at 9:17 AM
Assuming you hava a "Quote" class with a "text" property.
1
Quote $q:=new Quote;
The following are equivalent expressions:
1
2
// get property "text" of the quote
3
$p.text;
4
5
// the '.' is actually an infix notation for the GETPROPERTY function
6
GETPROPERTY($q,'text');
Because GETPROPERTY function returns a Pointer you can actually use that function at the left hand side or the right hand side of an assignment.
1
2
// read access
3
String $whatEver:=GETPROPERTY($q,'text');
4
5
// write access
6
GETPROPERTY($q,'text') := 'Not everything that is possible should actually be done!';
And you could also use that function in a binding expression
1
GETPROPERTY($q,'text')
Now:
1) if you use the function with a static property name you'll notice that the validation of a script change the code back to the infix DOT notation.
2) if you use the function with a variable property name you will of course loose the dependencies to that property!
1
String $name := 'text';
2
GETPROPERTY($q, $name);
So,
DO NOT USE THE EXPLICITE NOTATION
unless you have a VERY GOOD REASON to do so.
And trust me, for regular Appway users there is no such good reason!
n/A
replied
on April 24, 2014
at 9:54 AM
n/A
replied
on April 24, 2014
at 9:54 AM
Thanks a lot for the detailed explanation.
Please
sign in
to add a reply
About this site
|
Terms of use
|
Privacy statement
|
Contact us
© 2025 Appway AG. All Rights Reserved.
For example, $myObject.one, $myObject.foo, $myObject.bar where one,foo,bar is originally contained in a variable
Thanks