Get the latest tech news

Dtype_dispatch: a macro that defines macros that define enums and match them


dtype_dispatch: a most beautiful hack - Graph All The Things, a math blog by Martin Loncaric

Eventually I closed shop on PancakeDB (entirely because of the match clauses, of course), but I would occasionally still encounter this problem while writing dynamically-typed FFI libraries for Pcodec. I found some unsatisfactory tricks to reduce boilerplate, using true dynamic dispatch with Box<dyn ...> in places and lousy macros in others, but it was still a pain. And ~2 months ago I decided to use dynamic types internally to Pcodec (to support new features and also reduce binary size (each unnecessarily specialized function is duplicated several times in the machine code, you know)).

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Macro

Macro

Photo of Macros

Macros

Photo of Enums

Enums

Related news:

News photo

Rhombus: A New Spin on Macros (2023)

News photo

Unhygienic and referentially opaque macros with Scheme syntax-rules [pdf] (2013)

News photo

Quote-unquote "macros"