Walter Smith wrote:
> I understand that the XML support in Scala is for the most part just
> libraries... all but one exception: XML literals have to be supported
> directly by the compiler.
>
> Has there been some thoughts about letting libraries define literals,
> somewhat similar to implicit conversions? Not only XML literals, but
> numbers, strings, etc. just as well?
>
I'm not aware of much serious discussion--let alone work--on the idea
since this thread way back in Dec. 2007:
http://www.nabble.com/-scala--JavaPolis-Interview-tc14437381.html#a14447135> For JDK 7 they discuss allowing underscores within numeric literals to group
> them, and binary number literals, and list, set, and map literals. Scala can
> do the latter already... and not as literals but as library defined methods!
> If it was possible to define your own literals, DSLs could get even more
> natural: Writing 25ยบ18'25" would be very regular for nautical applications.
> Not to mention a lot of mathematical, physical, or chemical symbols that
> exist in the Unicode character set.
>
A lot of Unicode characters are already perfectly valid in Scala
identifiers, but you'll run into problems pretty quickly when you try to
use a syntax that's idiomatic in its own problem domain but not very
similar to Scala's syntax, as flexible as that can be.
> I'm quite sure anyone involved with writing compilers will start to shiver
> at the mere thought, but maybe it could turn out just as elegant and
> never-to-have-been-imagined-before like implicit conversions do. I think it
> would make the language even more regular... but maybe I'm wrong.
>
Doing the simplest thing that could possibly work (e.g. think up and
implement an escape syntax, build a framework that allows
Tuple2[Pattern, Function1[String, DSLNode]] or somesuch to be registered
with the compiler) is probably not a huge undertaking (he said glibly)
but there would no doubt be some very tricky bits, e.g. embedded Scala
expressions <like>{this}</like>.
-0xe1a