Today's challenge:
f=Function({}, {1,2,3}); tmp=f(); y=tmp[1]; // works x=f()[1] // doesn't work
understanding the issue helps a lot to understand how JSL works : - )
the inverse problem:
names default to here(1); x=1; Eval(x=1); // "does the same" x= Expr(hello); Eval(x= Expr(hello)); // fails
Does Head evaluate its argument?
concerning assign, does it evaluate its arguments?1. arg: no2. arg: yesIf you need to evaluate an expression "manually" [before taking the fist item from the result], use Eval():
x=f()[1] // doesn't work x=Eval(f())[1] // works
concerning glue:any args: yes
.... and Eval:yes, of course!besides that, there is an additional evaluation of the return value *) - after a first evaluation of the argument. So
Eval(print(1);x= Expr(hello);print(x); Expr(print(2)));
correctly assigns x=Expr(hello) and prints:
*) -> link / documentation / community?
If you need to evaluate an expression "manually" [before taking the fist item from the result], use Eval():</CODE><HR /></BLOCKQUOTE> <P>In my opinion that creates code which is difficult to read and should not be used, just return the value and use that. If you know you want just a specific index (<STRONG>and know how many there are</STRONG>) you can use a list and "throw away" the rest</P> <P> </P> <PRE><CODE class=" language-jsl">Names Default To Here(1); return_list = Function({}, {Default Local}, return({1,2,3})); {res, _, _} = return_list(); show(res, _); correctly assigns x=Expr(hello) and prints: In my opinion that should result in an error (currently it is in my opinion unexpected behavior). My guess is that this works due to how Glue behaves (it returns the last result and it then gets evaluated) Names Default To Here(1); Eval( x = Expr(hello); Print(x); );<BR />// Eval(Glue(Assign(x, Expr(hello)), Print(x))); vs (Name Unresolved: hello in access or evaluation of 'hello' , hello/*###*/) Names Default To Here(1); Eval( x = Expr(hello); );<BR />// Eval(Assign(x, Expr(hello))); I feel like this is what should always happen if Eval is being used in a case like this (attempt to evaluate Expr() -> fail evaluating it -> throw error). @EvanMcCorkle . View more... (Highlight to read) Names Default To Here(1); a = 1; Try( Eval( x = Expr(hello); y = Expr(a); ); , Write("\!NFirst Example: ", exception_msg); ); Try( Eval( y = Expr(a); x = Expr(hello); ); , Write("\!Second Example: ", exception_msg); ); Names Default To Here(1); a = 1; Try( Eval( x = Expr(hello); y = Expr(a); ); , Write("\!NFirst Example: ", exception_msg); ); Try( Eval( y = Expr(a); x = Expr(hello); ); , Write("\!Second Example: ", exception_msg); ); If you wish store just the hello name in variable, use Name Expr() Names Default To Here(1); x = NameExpr(hello); Scripting Guide > Programming Methods > Advanced Expressions, Macros, and Lists this does have some mistakes but overall it is correct and a good read.
</CODE><HR /></BLOCKQUOTE> <P>In my opinion that creates code which is difficult to read and should not be used, just return the value and use that. If you know you want just a specific index (<STRONG>and know how many there are</STRONG>) you can use a list and "throw away" the rest</P> <P> </P> <PRE><CODE class=" language-jsl">Names Default To Here(1); return_list = Function({}, {Default Local}, return({1,2,3})); {res, _, _} = return_list(); show(res, _);
In my opinion that should result in an error (currently it is in my opinion unexpected behavior). My guess is that this works due to how Glue behaves (it returns the last result and it then gets evaluated)
Names Default To Here(1); Eval( x = Expr(hello); Print(x); );<BR />// Eval(Glue(Assign(x, Expr(hello)), Print(x)));
vs (Name Unresolved: hello in access or evaluation of 'hello' , hello/*###*/)
Names Default To Here(1); Eval( x = Expr(hello); );<BR />// Eval(Assign(x, Expr(hello)));
I feel like this is what should always happen if Eval is being used in a case like this (attempt to evaluate Expr() -> fail evaluating it -> throw error). @EvanMcCorkle .
Names Default To Here(1); a = 1; Try( Eval( x = Expr(hello); y = Expr(a); ); , Write("\!NFirst Example: ", exception_msg); ); Try( Eval( y = Expr(a); x = Expr(hello); ); , Write("\!Second Example: ", exception_msg); );
If you wish store just the hello name in variable, use Name Expr()
Names Default To Here(1); x = NameExpr(hello);
Scripting Guide > Programming Methods > Advanced Expressions, Macros, and Lists this does have some mistakes but overall it is correct and a good read.
Thanks for the additional examples and your thoughts.