Here my general focus is Scala and Lisp/Scheme macros, not exactly the ones in the C/C++/Obj-C
I just don't see the point.
The way I understand it, is that macros are there to extend the language. But so are functions.
I understand that some things can't be implemented cleanly due to some language limitations, and as such a macro needs to be the way to go. But a LOT of examples I see with macros seem to be things that are fairly simple to implement using normal functions.
So what exactly is the purpose? Clean macros or otherwise, Someone please enlighten me. If possible, please provide some example code of something that can be done in macros, but is impossible/hard to do with normal functions.
A macro is an automated input sequence that imitates keystrokes or mouse actions. A macro is typically used to replace a repetitive series of keyboard and mouse actions and used often in spreadsheets and word processing applications like MS Excel and MS Word.
The benefits of counting macros and how to do it. Counting macros can help someone make sure they are eating the right ratio of nutrients and maintain a moderate weight. Macronutrients, or “macros,” are proteins, fats, and carbohydrates. They are essential nutrients that provide energy and help keep people healthy.
The macronutrient amounts that you eat are what determine total calories consumed. The macro diet focuses on tweaking those within your estimated calorie needs to determine what macronutrient ratio most effectively supports weight loss and fuels the body.
There is a concise summary of what things have been done with Scala macros: http://scalamacros.org/paperstalks/2014-02-04-WhatAreMacrosGoodFor.pdf. To sum it up, macros are known to be good for: 1) code generation, 2) advanced static checks, 3) empowering domain-specific languages. Having macros in Scala to be type-based provides an additional and powerful twist to the capabilities typically found in macros in Lisp-like languages.
Some examples in the slides linked above can indeed be implemented without macros, but the result will be either lacking in some sense (e.g. in performance) or overly complicated for the users (e.g. because of heavyweight error messages). For example, typed channels for Akka could be conceivably implemented with pure implicits, but compilation speeds and understandability would suffer. Or, scala/async could be implemented as a compiler plugin, but then it would have to depend on internal compiler APIs and would be harder to distribute.
Of course, macros are not the silver bullet. There clearly are use cases when they are not the best choice, and this is something that's outlined in http://scalamacros.org/paperstalks/2014-03-01-MacrosVsTypes.pdf. What's curious, though, is that in a number of situations neither pure-macro, nor macro-less solutions, but rather carefully constructed hybrids end up being the best.
In both Common Lisp and Scheme most of the special syntax are indeed implemented in terms of other special syntax, thus macros.
For instance both Scheme and CL have if
, cond
and case
but only if
is a primitive syntax.
There is nothing special about macros defined by the standard and the ones you might make yourself. They can be made to behave and work just as good as primitives.
Macros has a cost of obfuscating and surprising the reader. Using common naming conventions like with-*
might help a bit but one should never use a macro if a function/procedure can do the job or if the form will only be used in a few number of places.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With