Stephen Colebourne's blog: User-defined literals in Java?
Java has a number of literals for creating values, but wouldn't it be nice if we had more?
Current literals
These are some of the literals we can write in Java today:
- integer -
123,12s,1234L,0xB8E817,077,0b1011_1010 - floating point -
45.6f,56.7d,7.656e6 - string -
"Hello world" - char -
'a' - boolean -
true,false - null -
null
Project Amber is also considering adding multi-line and/or raw string literals.
But there are many other data types that would benefit from literals, such as dates, regex and URIs.
User-defined literals
In my ideal future, I'd like to see Java extended to support some form of user-defined literals. This would allow the author of a class to provide a mechanism to convert a sequence of characters into an instance of that class. It may be clearer to see some examples using one possible syntax (using backticks):
Read full article from Stephen Colebourne's blog: User-defined literals in Java?
No comments:
Post a Comment