Proposal: Add field puns/map shorthand to Elixir

proposal
on-topic-only-please

#106

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
  ...
end

Basically just a prefix that translates the whole module call.


#107

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


#108

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.


#109

Exactly!

~ 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:


#110

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

require Punning
Punning.defmodule Module do
  ...
end

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


#111

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.


#112

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 https://dev.realworldocaml.org/records.html.


#113

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.

https://downloads.haskell.org/~ghc/7.6.3/docs/html/users_guide/syntax-extns.html#record-puns