To me this is like a crutch that the language itself cannot solve.
Ultimately, Scala will be held back because of Java. Scala deserves to live on by itself.
To me this is like a crutch that the language itself cannot solve.
Ultimately, Scala will be held back because of Java. Scala deserves to live on by itself.
(defun add-two (x) (+ 2 x))
to: (defmacro add-two (x) `(+ 2 ,x))
(And much more complex macros are still readable if you know the rest of the language.)Whereas in Scala the macro facility looks different enough from vanilla code to have to be learned separately.
Backquote-comma-splice is so useful for building symbolic trees that it's an example of what Whitehead famously celebrated:
http://introtologic.info/AboutLogicsite/whitehead%20Good%20N...
Off topic, but if you read that piece it's interesting how much of what he says applies also to programming, yet how controversial it remains in the realm of programming languages.
(defmacro add-two (x) (quote ((quote +) 2 x)))
Not as clean, but not awful. Everything that should have a comma doesn't, and everything that shouldn't have a comma has a QUOTE.So yes, I really like the `,@-style notation, but also think the alternative is tolerable.
(It bugs me that I can't think of a simple example that must be a macro (and not a function), but you get the idea.)
It's interesting, though, that this is a usability question rather than a technical one, i.e. you can build the same expressions without that notation, just not as easily. But that's what's great about that Whitehead piece I linked to: he shows how fundamental this is. It seems like a surface matter but isn't, because it influences what other things one can use one's limited mental capacity to do. It makes subsequent thoughts possible that wouldn't otherwise be. (Edit: this is why programmers who talk about how language doesn't matter are deeply wrong. And also why programming languages have surrounding cultures. But I go off topic again.)
By the way, your macro example is off, since there's nothing to cause X to be evaluated; it's forever embalmed in that outer QUOTE.
Implementing IF in terms of COND or vice-versa.
> (It bugs me that I can't think of a simple example that must be a macro (and not a function), but you get the idea.)
My main three rules: 1) delayed, conditional, repeated, etc. evaluation of arguments 2) compile-time computation for massive optimization potential 3) running or inserting code before or after any of the argument forms (useful for tests--init clean env, run test body, destroy env) or changing the argument forms themselves.
# without macro
def addTwo(x: Int) = x + 2
# with macro
def addTwo(x: Int) = macro addTwoImpl
def addTwoImpl(c: Context)(x: c.Expr[Int]): c.Expr[Int] = c.reify(x + 2)
It used to look like this in earlier proposal: macro def addTwo(c: Context)(x: c.Expr[Int]): c.Expr[Int] = c.reify(x + 2)
Here c.reify is similar to quoting in lisp version. It really should have been something like: macro def addTwo(x: Expr[Int]): Expr[Int] = reify(x + 2)
It almost feels like they are making syntax more cumbersome on purpose to make inexperienced people not to use it. On the other hand they may require explicit Context to have purer API from their stand point. From end-user perspective it would be much simpler to have it in some singleton class as there most likely will never be two different contexts in a real-life macro. sub add-two ($x) { 2 + $x }
to: macro add-two ($x) { quasi { 2 + {{{$x}}} } }