Comments (4)
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.
from haxe.org-comments.
haxe compiler to produce an AST (may be you know=?)
See "dump" flags here: https://haxe.org/manual/compiler-usage-flags.html
from haxe.org-comments.
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.
from haxe.org-comments.
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
from haxe.org-comments.
Related Issues (20)
- [haxe.org/manual] Authors and contributions HOT 1
- [haxe.org/manual] Interfaces HOT 1
- [haxe.org/manual] new HOT 2
- [haxe.org/manual] Implementation details (Standard Library - Json) HOT 1
- [haxe.org/manual] Dynamic (Class Fields - Access Modifier) HOT 10
- [code.haxe.org] Abstract types - Email address as abstract type HOT 1
- [code.haxe.org] Beginner - Pattern matching HOT 2
- [code.haxe.org] Abstract types - Pipe using Abstract Operator Overloading HOT 2
- [haxe.org/manual] Target Details HOT 1
- [haxe.org/manual] Generic HOT 2
- [haxe.org/manual] Null Safety HOT 2
- [haxe.org/manual] Blocks HOT 3
- [haxe.org/manual] Tail Recursion Elimination (TRE) HOT 1
- [haxe.org/manual] Module Sub-Types HOT 3
- [code.haxe.org] Macros - Working with compiler flags HOT 1
- [haxe.org/manual] Extractors HOT 2
- [haxe.org/manual] Array Access (Types - Abstract) HOT 2
- [haxe.org/manual] Overriding Methods HOT 1
- [haxe.org/manual] Variable capture HOT 2
- [haxe.org/manual] Tracing Types HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from haxe.org-comments.