views:

128

answers:

1

Is it possible to create types like e.g. String(20) in scala?

The aim would be to have compiler checks for things like:

a: String(20) 
b: String(30)
a = b; // throws a compiler exception when no implicit conversion is available
b= a; // works just fine

Note: It doesn't need to be/named String

+8  A: 

Yes, it is possible, using Church Numerals encoding. Jim McBeath has a good series of blogs relating to that. Start here, but keep looking for other references on the Internet. He wasn't the only one to talk about it, nor was this his only post.

It isn't, however, very much practical.

Daniel
Hopefully one day "real soon now" somebody will roll up their sleeves and write a Scala compiler plug-in to support "Refinement Types" in the Style of Sage http://sage.soe.ucsc.edu/ This will require integrating a latest-greatest theorem prover of course :-)
Alex Kravets