Open utterances-bot opened 5 years ago
Hi, I learn a lot about the language with this short article, thank! Indeed haxe behaves allows expressions that others don't, but still, i'm not convinced with the statement "everything is an expression". In JavaScript which abstract syntax nodes I'm familiar with you have roughly three kind of nodes that appears as statements
foo(1); // ExpressionStatement (and inside it has a CallExpression node) try{} catch(e){} // TryStatement class C{} // ClassDeclaration
In my experience, the first ones are special (probably a helper of the language that considers those some kind of statements also valid expressions so it's easier to work with) - smells like artificial..
The second kind the ones ending with "Statement" are very rigid and unlike haxe as you teach me I don't htink can be used as expressions, never , like ifStatement, VariableStatement, and others.
Nevertheless I think in this case is haxe which is doing something artificial, like a helper since no matter how I try to write it always fails to parse if using braces or semicolons :
trace(try{return 1;}catch(e:Dynamic){2:}) // fails
Also I think your example that don't have semicolons is only valid code if used as as expression but not as statement. So it's kind of confusing but I see the utility. What would like to be use is if every satement with 1-lenfth sub blocks and no semicolos like that will do thi smethamorphosis or only some...
And finally the Declarations, that I'm not sure if they are expressions or statements in JS but what I do know is that in this particular case JavaScript allow to use them as expressions while haxe won't:
class C extends class B{} {}
That's valid JavaScript using a classDeclaration inside a class declaration - I didn't found a way of parsing something similar in haxe.
So in conclusion, thank's for make think and experiment and learn. Although the title of the article is miss leading since it first made me think that haxe wasn't an imperative language with statements -
And more importantly, these are some online AST parsers for many languages (the last one is mine that's why so ugly)
https://astexplorer.net/ https://cancerberosgx.github.io/demos/univac/playground/
It helps me a lot understand a language by playing a little bit with these viewers, unfortunately I didn't found a way to use haxe compiler to produce an AST (may be you know=?) -I think that's the real way of full comprehend what is what and - and I find out learning a little bit about the language is useful sometimes too.
Thanks again.
haxe compiler to produce an AST (may be you know=?)
See "dump" flags here: https://haxe.org/manual/compiler-usage-flags.html
Nevertheless I think in this case is haxe which is doing something artificial, like a helper since no matter how I try to write it always fails to parse if using braces or semicolons :
trace(try{return 1;}catch(e:Dynamic){2:}) // fails
Well, you do have a typo in your example (you put a colon after 2
instead of a semicolon). But using try
as an expression works:
trace(try 1 catch (e:Dynamic) 2); // outputs 1
trace(try { 1; } catch (e:Dynamic) { 2; }); // outputs 1
trace(try { trace("aaa"); 1; } catch (e:Dynamic) { 2; }); // outputs aaa, then 1
As you can see, the generic "form" of the try
expression is try <sub-expression>
, followed by any number of catch (<exception identifier>:<exception type>) <sub-expression>
. If you want to do multiple things in sequence in the try
sub-expression, you simply use a block, which is also an expression - as demonstrated in the second and third examples.
And finally the Declarations, that I'm not sure if they are expressions or statements in JS but what I do know is that in this particular case JavaScript allow to use them as expressions while haxe won't:
class C extends class B{} {}
That's valid JavaScript using a classDeclaration inside a class declaration - I didn't found a way of parsing something similar in haxe.
Yes, there is no way to do this in Haxe, nor are there plans for this. Something vaguely similar to anonymous classes (like you describe) is only allowed in macros as class reification.
In non-macro code, however, classes are always declared in modules and will be accessible via the module + class path as normal.
So yes, class declarations and various language constructs (package
, import
, …) are NOT expressions, but there is no sane way to treat them as such. In JavaScript, a class declaration is syntactic sugar for a special function declaration. Haxe targets many platforms where a "class" has wildly different semantics, so it cannot be treated as an expression.
Thanks for the detailed explanations, really appreciated ! I understand haxe have many more compromises than most languages and is not that I will use "inlined" expression syntax everywhere but clean lots of doubts and help me learn a lot, from the article and your comments, specially that now I have the AST now, thank again
Everything is an expression - Principles - Haxe programming language cookbook
Many programming languages split code into two kinds of elements: statements and expressions. Statements perform some action (e.g. if/else) and expressions return values (e.g. a + b).
https://code.haxe.org/category/principles/everything-is-an-expression.html