Is it possible to create dimensional types in Scala?

Is it possible to create types like, for example, String(20)in scala?

The goal would be for the compiler to check 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 does not need to be / named String

+3
source share
1 answer

Yes, it is possible using Church Numerals encoding. Jim McBath has a good series of blogs related to this. Start here , but keep looking for other links on the Internet. He was not the only one talking about it, and was not his only message.

However, this is not very practical.

+8
source

Source: https://habr.com/ru/post/1740492/


All Articles