Proposal: Add field puns/map shorthand to Elixir



You know, having % be sigil-like thing could be quite nice! We also really really badly need a way to embed sigils with the same delimeters so we could do things like ~s{ blah {} thing } for delimeters that have matching part, maybe that could be built in to % as a sigil (with the ‘empty’ % ‘sigil’ being normal map operations).

Best I’ve really seen is just:

import MyMacros
themacro defmodule ModuleName do

Basically just a prefix that translates the whole module call.


That is even more succint than my idea! :slight_smile:


I’m not sure I agree. The whole idea of sigils is that they are like strings. What you describe looks like tokenization macros, which are probably more powerful than they should.



~ Sigils are for string-like things, % p-sigils would be for embeddable things, still passes strings to the call but it requires things to be ‘closed’, so everything like { requires a matching } before closing and so forth, the purpose being for DSEL code generation instead of just string stuff, which would suit such map-like things well as you can still use %{a: 42} for a normal map or %{punned}p for a punning map or so (or where-ever to put the indicator). :slight_smile:


Nice idea, or even shorter (create a new defmodule definition):

require Punning
Punning.defmodule Module do

Still nog very elegant though if you need to do that for every module.


This is what I tend to do but I really shouldn’t to be honest. Doing it this way means you can’t compose them together.


Where does the term “punning” come from?

I haven’t been able to find any other references to this phrase besides this page for an OCaml book


I don’t know. I suspect OCaml is the source. Haskell has it as an extension. Rust has it built in, but they seem inconsistent in calling it field puns. Javascript has it, but it’s just lumped in with destructuring.