User-defined literals

kdex kdex at kdex.de
Fri Oct 14 20:26:32 UTC 2016


I haven't seen this discussed on ESDiscuss before. For those not 
familiar with user-defined literals: Essentially, they allow certain 
literals (e.g. 123.45, "hello") to produce a user-defined object by 
marking the literal with a user-defined suffix.

They were introduced in C++11 [1], and I'm not sure if any other 
language has implemented them.They were also discussed on Python-ideas [2].

Some potential use cases:

- Since ES2015, built-ins are subclassable, so you could use a literal 
to call custom constructors (e.g. for custom string objects)
- Units (e.g. `100m + 2cm`)
- Numbers may be specified in arbitrary bases (e.g. `124_9 === 
Number.parseInt(124, 9)`)
- Complex numbers, quaternions, … (you could make these even nicer with 
operator overloading)

Any opinions?


— kdex

[1] http://en.cppreference.com/w/cpp/language/user_literal
[2] https://mail.python.org/pipermail/python-ideas/2015-June/033871.html


More information about the es-discuss mailing list