3 ms·
I'm not that familiar with Scala, but the examples aren't very convincing. Why must `printf` be defined as a macro? Why not a plain old function? What are the b
by nwjsmith 15y ago
I'm not that familiar with Scala, but the examples aren't very convincing. Why must `printf` be defined as a macro? Why not a plain old function? What are the benefits to the second example?
The other thing that bothers me is the empty 'Use cases' page. Scala macros might be a solution to a problem nobody has. A problem that people _do_ have is the breadth of Scala language, and macros will add to the conceptual dog pile.
- mike_esspe 15y agoYou can make printf type safe with macro, so it will complain at compile time if you are passing incorrect type.
- bickfordb 15y agoThis lets you embed programs in other languages (DSLs) within your program that are parsed and compiled at compile time instead of run-time. This has the following benefits over run-time DSLs / languages embedded inside of strings: 1. The embedded language will be type checked at compile time. For example a regular expression macro could enforce that your regular expression has balanced parentheses in capture groups. This potentially lets you shift a class of errors from run-time to compile time. 2. The language will be parsed at compile time saving run-time cycles. 3. The host language can be extended and experimented with without adding new syntax. Features like this actually limit the growth of the "dog pile", since many language experiments features can be tested as macros first. Some common DSL examples are regular expressions, parser generators, SQL / ORM generators and LINQ
- bad_user 15y agoAn impressive example of what you could do with macros is LINQ - a static and type-safe DSL for querying collections of any kind. People did impressive things with it, like offloading the work to the GPU. Another example would be about adding your own improvements to the time-system, improvements that are actually going to help you keep the code safe. Like the units of measure from F#. However, adding powerful macros to a language that was not designed for macros and that is NOT simple ... I do think it is a bad idea. I'd be happy to see in Scala something like the code quotations from F#. Anything less is useless, anything more will make Scala way too complicated.
- dxbydt 15y ago> Scala macros might be a solution to a problem nobody has Pls read slide 10 of this document - http://scalamacros.org/talks/2011-10-18-WhatsUp.pdf http://scalamacros.org/talks/2011-10-18-WhatsUp.pdf --Proposed Code snippet with macro --- class Queryable[T, Repr](query: Query) { macro def filter(p: T => Boolean): Repr = <[ val b = $this.newBuilder b.query = Filter($this.query, $reify(p)) b.result ]> } val products = db.products products.filter(p => p.startsWith("foo")).toList -- My takeaways : 1. adding LINQ support to Scala via macros 2. macros virtualize XXX, where XXX = vals, vars, defs, types, classes, traits. So macro XXX can be used in place of XXX. The author also says "We can tailor macros to fit your particular use case". He mentions OR-mapping, and a Scala-to-javascript compiler that might end up using these macro invocations, among other things.
- scalamacros 15y agoThanks for pointing out the weak spots of the content! We've posted several compelling use cases enabled by macros (http://scalamacros.org/usecases/index.html http://scalamacros.org/usecases/index.html) and added some more information to the main page that uncovers the inner beauty of the examples. Btw, we realize the responsibility of adding a competely new feature to Scala, so every new macro-related idea is being weighed against the complexity it brings to the table. So far we've done away with several really interesting prospects in order to keep the overall proposal simple.