I'll put forward conditional compilation based on Scala version. This
is something that has direct impact on 3rd party libraries. Of course,
it wouldn't help them with code *pre*-macros, so it wouldn't be of
immediate use.
--
Daniel C. Sobral
I travel to the future all the time.
Excellent project, taking inspiration from an excellent (from what I
have read) source.
> Having said that, I'd like to find compelling use-cases for macros, so
> that the functionality implemented will be immediately useful. Of
> course, macros are very elegant in themselves, and, from the standpoint
> of a language designer, I can name at least a handful of cool features
> that can be enabled by macros. However, what I'd really like to know is
> what scenarios are the most important to you in your everyday jobs. I
> would be very glad to receive your feedback!
One obvious point it in Scala itself, to avoid generating
Function1.scala to Function22.scala, idem for Product<nn>.scala and
Tuple<nn>.scala
Enum is another good case, eg. to automatically generate an index number
from a set of enums. I made a workaround by putting the case object
names in a list, but I dislike duplicating these names.
I can see them useful to make DSLs, too.
There is a little intersection with partial functions, for example, but
still I think it can be useful there.
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
That assumption is not really supported. Eclipse's ScalaIDE 2.0 used
*the compiler* to figure out Scala code. It, therefore, knows as much
as the compiler does. Its problems lie elsewhere.
It would be true of IDEA, presumably, but we'll have to wait and see.
It's not like such a feature could hit trunk tomorrow.
These days I am able to see language extensibility working in Scala, and
it actually seems to work, but not in the way I would have dreamed of
even 15 years ago.
Now we are talking about macros again, and I naturally wonder about such
things as how do you know when there is an error in the macro vs. the
program, or how does the debugger work properly. I'm not saying macros
are a bad idea, but what has fundamentally changed to make people so
positive about them now? Is it that computing science has advanced to
the point where where now know how to do them better?
Cheers, Eric
It is a failure in C because it is unsafe, disregards types, can lead to
incorrect code (eg. #define M x*x instead of (x*x)) and so on.
Not sure about macros in C++, as I never used them.
> I'm not saying macros
> are a bad idea, but what has fundamentally changed to make people so
> positive about them now? Is it that computing science has advanced to
> the point where where now know how to do them better?
You mentioned only a part of computer history, but to my knowledge,
macros in Lisp (and derivatives) and some other languages, so called
hygienic macros, are safe to use and are seen as powerful features. And
it goes back far in the said history.
There might be downsides to the use of macros, perhaps debugging,
readability, I don't really know (I have yet to use a PL with good macro
support, but I like the idea).
I believe that macro support would strongly reduce the need for compiler
plugins, which need to be installed in your Scala distribution and thus
are harder to use in a project.
Scala might need macros less than some other languages (eg. you can
define a repeat(n) partial function, while other languages would have
used a macro to make new structures like that) but I believe it can
still be useful. I might be interesting to take a look at the existing
compiler plugins and see if a macro system can really replace them (or
simplify them).
Macros are about manipulating the AST, to generate new code.
Another interesting feature I though of:
Parse a format string and check it at compile time, instead of run-time.
We then go a step ahead in type safeness.
Ie., define format as a macro, and verify that in:
format("%s value is %d", name, name.value)
name is really a string, and name.value is an int.
And generate directly the StringBuilder stuff, instead of relying in
runtime code walking the string in search of %
Idem for a PHP-like templating system:
"Foo $name bar ${x map (_.toUpperCase)}" or similar: compile time check,
optimized code.
Idem for regular expressions.
Along with Scala's multiline strings, it can be powerful and efficient.
On 17/09/2011 13:13, Eugene Burmako wrote:
Another interesting feature I though of:Having said that, I'd like to find compelling use-cases for macros, so
that the functionality implemented will be immediately useful.
Parse a format string and check it at compile time, instead of run-time. We then go a step ahead in type safeness.
Ie., define format as a macro, and verify that in:
format("%s value is %d", name, name.value)
name is really a string, and name.value is an int.
And generate directly the StringBuilder stuff, instead of relying in runtime code walking the string in search of %
Idem for a PHP-like templating system:
"Foo $name bar ${x map (_.toUpperCase)}" or similar: compile time check, optimized code.
Idem for regular expressions.
Along with Scala's multiline strings, it can be powerful and efficient.
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --
Yes, but, to illustrate my point, say macros were introduced in 2.9.
You wouldn't be able to use them to avoid this problem, because 2.8
wouldn't accept the macro.
Oh, I *hope* it will be useful for that! I know Odersky wants to put
breaks on wild changes -- and that's good -- but there's still much to
be improved on Scala.
> A totally random and possibly incorrect thought: Macros in C are fairly
> comparable to a Templating System in a modern Web App
Umm, no they aren't. C macros provide static compile-time textual
substitution, templating systems provide run-time dynamic output formatting.
I'm not at all convinced that macros are a good idea for Scala to be
honest, I certainly don't miss them in Java. However, that's probably
due to the scars they have inflicted on me in C/C++, so perhaps
something less lethal can be devised for Scala.
--
Alan Burlison
--
I am not sure how to conflate the two sentences.
In a number of languages using (hygienic) macros I looked at (Nermele, Seed7, some others
I don't recall), macro usage doesn't look like a function call in general, but rather
allows to use them to make some kind of DSL, eg. to make new operators, new loop
structures, etc.
Now, perhaps one problem is to stumble upon such macro in some code and to be stumped
because it might precisely not look like the regular language, but offers no hints it uses
some macro defined somewhere...
Fwd to list
Tbh I'd much rather have unification of tuples and parameter lists, abstraction over arity and abstraction over constructors.
Cheers,
V
Please see Template Haskell. I would hate for us to repeat mistakes out
of ignorance of lessons already learned.
(not directed at any comment in particular)
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJOdzBcAAoJEPxHMY3rBz0PIjAH/0YwIH3b+CHtjRyWvC5xg2FP
cvL2rTwELj52tHDy5e7+4/1um2nmy1T6xTdaDF+T3UdofKRnoAYDK3tUKpX9Tv/j
m5er0Te6rqR+bw3x0B2wdZEpCwXuMlaNWnfn74uONs7hpB1EMkx0h6BmHtKYJtdN
iS3bP8hgrMcThchK+VBmWjoJkup3doulbttEZSwa2w4gNc+T6XwSIDQoJ+DOYdgA
ESrJu27Vc5rrctVb4De2iuspucp/GlHLq+rgbCie7EfQWkXMzB1wD0vuN9MHB1mk
gqPphx0HKtrsNPwDIk0yxj+q08v2yfPybdf0J3b2MKJBdgRu+7rAzzsAhIFCb0Y=
=rkgK
-----END PGP SIGNATURE-----
I am not sure how to conflate the two sentences.
In a number of languages using (hygienic) macros I looked at (Nermele, Seed7, some others I don't recall), macro usage doesn't look like a function call in general, but rather allows to use them to make some kind of DSL, eg. to make new operators, new loop structures, etc.
Now, perhaps one problem is to stumble upon such macro in some code and to be stumped because it might precisely not look like the regular language, but offers no hints it uses some macro defined somewhere...
Please see Template Haskell.
On Nemerle macros normally appear as either functional calls or
annotations -- which seems ideal for Scala. They can _also_ add
syntax, by defining how the parameters are separated by literal
tokens, but I doubt that can be achieved.
See http://nemerle.org/macros.html. One of the first examples show the
macro being used function-call like, and the first for-loop example
shows that as well. And, then, there's the serializable example, which
is interesting because it shows the (same) macro being inserted at two
points in the compiler phases, first to modify the type, and, then, to
add a method definition when all types are already settled.
>
> Now, perhaps one problem is to stumble upon such macro in some code and to
> be stumped because it might precisely not look like the regular language,
> but offers no hints it uses some macro defined somewhere...
>
> --
> Philippe Lhoste
> -- (near) Paris -- France
> -- http://Phi.Lho.free.fr
> -- -- -- -- -- -- -- -- -- -- -- -- -- --
>
>
--
Love to see you tackle this - I have been wanting this for a long time!
Usage examples:
* adding change notification support to classes
* 'cached' semantics for defs - store each result in some sort of cache
(that can be passed in as an additional implicit parameter) and evaluate
the def exactly once for every combination of parameters
* the @beanGetter / @beanSetter stuff from the compiler itself
* DSLs, especially for interop with typical Java frameworks, require
code to be generated rather than wrapping stuff at runtime
- - Arno
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk53XRoACgkQbmZsMyUPuXT+PQCg7t4zqqy1JKUCRGZ0pImInKOY
tIYAmwSGToKU9o32HiI/SnZ7iwUQu4vN
=wGXv
-----END PGP SIGNATURE-----
Thanks for the correction, although I guess what I really was trying to say both are basically textual substitution systems. It seems thatthe macro systems in LISP, Scheme, etc are a bit less braindead and hence might actually be a neat idea in Scala :-)
On 09/20/2011 07:40 AM, Ken McDonald wrote:
> (many setter/getter type
> functions which only differed in the name and type of the property they
> operated on), and to do so, wrote a little Scala program than actually
> generated them. It would've been nice to have a macro system that could've
> done that.
:(
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQEcBAEBAgAGBQJOd7c6AAoJEPxHMY3rBz0P8WAIAMovYGO3/yqLkCBGWdBvWsAx
5jclBR1AayY4xAlrCbclQFL0pfinKaFM6+jFQAhqWoScSBzfszLiMVejfnXcexJo
b+cS53Cs0oPHN0DWl3iu0CIl0gqxzsjTrMGrU6emlDvDiv1ZwfLx+ibUkjEl8AQg
QEghgei6sWMAGCrO5ENkdPlUBiTNTIVRATgLfJc2bca6iH/TvGaRBvfI0wKfKPEW
5mHFkfFHO4cgvNRIUaVFf3GcX06AWCqHLxx5/k47Ud4IRNx0niLXDltp4iPnVQMb
dTMPV5IL0rNvXxyFXgdUu9HBa9qw6P1tjFLGnYm8yKCk543+RQlsdrwujdtPooo=
=qS+s
-----END PGP SIGNATURE-----
Thanks for all the feedback - please, keep it going!
@All. I'm not really sure whether it will be possible to implement
macros that change syntax (I have yet to dig into scalac parser to
find that out), though I'd really like to implement unrestricted code
generation (i.e. to support not only expressions, but also class/
method/implicit/whatever definitions).
One difference is that Scala is typed, and so would the macros. That,
plus hygiene, will go a long way towards increasing their safety.
> Please see Template Haskell. I would hate for us to repeat mistakes out
> of ignorance of lessons already learned.
what are (in your view) the lessons to be learned from Template Haskell?
- Tiark
Unless there is a compelling use case for doing the above, please don't do it! Scaisla syntax is already highly flexible due to a number of features; interchangeability of method invocations and operators, use of implicits, and several other things. Unless others can give a real use case for needing further syntactic flexibility, I think that additional such flexibility would make Scala harder to learn without adding much real value.Ken
Unless there is a compelling use case for doing the above, please don't do it! Scaisla syntax is already highly flexible due to a number of features; interchangeability of method invocations and operators, use of implicits, and several other things. Unless others can give a real use case for needing further syntactic flexibility, I think that additional such flexibility would make Scala harder to learn without adding much real value.Ken
Some DSLs would not play well with scala syntax.In Graham's On Lisp, he builds a syntax for Prolog in Lisp using macros. ( http://lib.store.yahoo.net/lib/paulgraham/onlisp.pdf chapter 24)In practice, this allows to build macro-based DSLs that are directly usable by domain expert with little to no knowledge of lisp.It is more difficult to do so, and sometimes impossible, without a flexible syntax. Scala syntax is flexible but still has numerous assumptions on thelanguage which make creating some DSLs quite difficult.
I am not sure how important what will be in F#, but it might worth
looking at it[0]:
- I think type providers-like functionality were already asked for
- Auto-properties might be a nice addition
- Maybe some people get familiar with LINQ, so being able to use a
similar construct for various tasks would be welcome
(- I think the measure types can be implemented as a library, but the
precedence rules can be offensive.)
Personally I do not see these as must have features, but I guess
Microsoft were made researches on the most wanted functionalities.
Cheers, gabor
[0]: http://msdn.microsoft.com/en-us/library/hh370982(v=VS.110).aspx
aka
subclass, proxy, and add another trait