Giter VIP home page Giter VIP logo

Comments (4)

cancerberoSgx avatar cancerberoSgx commented on May 31, 2024

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.

RealyUniqueName avatar RealyUniqueName commented on May 31, 2024

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.

Aurel300 avatar Aurel300 commented on May 31, 2024

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.

cancerberoSgx avatar cancerberoSgx commented on May 31, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.