Live data from Hacker News

Dart Macros and Focus

shorebird.dev

11–16 of 16 posts

Re: Dart Macros and Focus

#11
post #8
post #7

Earlier quoted context omitted.

You can think of macros as a compiler frontend. The compilation process is more or less: lexing -> parsing/macro expansion -> compilation of core language The macro expansion phase removes all uses of macros and produces a program in the core language (which has no macros). The idea is that a user can extend the language with new features. As long as a program that uses the new feature can be transformed into an equi…

> An often overlooked positive effect of macros is that the entire community can experiment with new language features. Sure, that sounds positive - but with enough macros, you can turn "your" version of the language into something completely unrecognizable to people that are only familiar with the "basic" version (or, otherwise said: congratulations, you've got yourself a DSL!), and I would say that's a rather negat…

This is no different from having competing libraries for dates, regular expressions, html parsing etc.

Re: Dart Macros and Focus

#12
post #9
post #6

Earlier quoted context omitted.

Generally meta features like macros are _far_ more useful to library authors that need to do some sort of "reflection" on your application code. Serialization libraries are frequently the largest beneficiary. In my ideal world I don't need to write macros or use language meta/reflection features while (only) writing code to solve business problems, but to get to that ideal often the libs you use do need reflection ca…

There's also the Lisp philosophy which emphasizes that you are your own libraries' author, too ! Third-party libraries can only help you with common business code - anything specific to your domain, or your business, you have to model yourself. Using macros is fundamentally not different than using classes or functions to create an environment[0] where expressing your business logic is straightforward, and invalid st…

[deleted]

Re: Dart Macros and Focus

#13

Can someone ELI5 or point me to an idiots guide as to why macros are desirable? As a non professional programmer who work on small side projects I'm struggling to understand what benefit they bring.

One of the main use-cases for compile-time metaprogramming (like macros) has been to be able to write performant code that does not type-check correctly in a typed language. Library writers encounter this issue frequently, e.g. the C++ standard library is heavily based on template metaprogramming. One example of code we want to write in a generic way are map and flatMap operations that you can define on lists, binary trees, hashmaps, rose trees and many other container-like data structures. But many typing system do not let you write the map and flatMap abstraction in a type-safe way once and for all. In dynamically typed languages, there is no such issue.

Some modern languages (Haskell, Scala) overcome the lacking expressivity for library writers with higher-kinded types and principled support for ad-hoc polymorphism (e.g. typeclasses), thus reducing the need for meta-programming. Notably, Haskell and Scala have unusually principled support for metaprogramming.

As a heuristic, I would suggest that using metaprogramming for small or medium sized normal ("business") code is a sign that something maybe be suboptimal, and it might be worth considering a different approach (either to the choice of implementing business logic or the chosen programming language.)

Re: Dart Macros and Focus

#15
post #8

Earlier quoted context omitted.

> An often overlooked positive effect of macros is that the entire community can experiment with new language features. Sure, that sounds positive - but with enough macros, you can turn "your" version of the language into something completely unrecognizable to people that are only familiar with the "basic" version (or, otherwise said: congratulations, you've got yourself a DSL!), and I would say that's a rather negat…

This is no different from having competing libraries for dates, regular expressions, html parsing etc.

That's the reason why many prefer "batteries-included" languages that provide a standard implementation for all the basic stuff...

Re: Dart Macros and Focus

#16

Can someone ELI5 or point me to an idiots guide as to why macros are desirable? As a non professional programmer who work on small side projects I'm struggling to understand what benefit they bring.

Macros are capable of much the same thing as reflection, but without the reflection part. Anything you would use an annotation/decorator/attribute/etc for, can be done as a macro to eliminate the runtime performance impact and the vector for action-at-a-distance. In addition to removing internal copy-pasting/templating when you just can't quite replace it with an interface instead.
Post reply on HN