Hacker News new | past | comments | ask | show | jobs | submit login

I think we can agree that given while() and for(), one is sugar because it can be defined by the other.

My observation is that while it's possible to say there is sugar, it's not possible to say which of the two is the sugar. That is, the while() can be defined in terms {language - while()} and for() can be defined in term {language - for()}.




Sorry, I should have been more clear. I'm suggesting that you call a piece of syntax syntactic sugar when it's actually implemented in terms of the syntax of the rest of the language. So it's possible that, e.g. while() is sugar in one C++ implementation and for() is sugar in another.

That's just a question of terminology, though. There's a paper by Felleisen[1] that makes what you're talking about precise. What you do is you ask weather an extended language can express a piece of syntax with respect to the base language. Your statement then becomes:

Let C* be C++, but without while() or for(). Then, (i) C* + for() can express while() with respect to C. (ii) C + while() can express for() with respect to C*.

And all of this is made completely formal, and it's used to talk about the expressive power of programming languages.

[1] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.51....


I agree, it's only a question of terminology. My comment was only that the definition as given was incomplete.

Thanks for the link. It's too much beyond me to really follow, but I've no doubt that such a definition is possible.

Would you say that Python's with statement is sugar? The PEP at https://www.python.org/dev/peps/pep-0343/ might suggest so, but Python has no way to generate new unique symbols for variables, so the translation of:

  with open(filename):
     pass
can't trivially be converted to:

        mgr = (open(filename))
        exit = type(mgr).__exit__  # Not calling it yet
        value = type(mgr).__enter__(mgr)
        exc = True
        ...
because of possible name collisions.


Renaming things isn't something you as a programmer should have to do; that should be handled behind the scenes by the desugaring system / macro expander. So yeah, I'd say that's a fine piece of sugar.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: