Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Prohibit allocation of value classes

According to the documentation on value classes, they may be allocated under a number of circumstances:

Allocation Summary

a value class is treated as another type.

a value class is assigned to an array.

doing runtime type tests, such as pattern matching.

Is there anyway to say,throw a compilation error if these circumstances occur?

like image 807
J Pullar Avatar asked Nov 09 '22 15:11

J Pullar


1 Answers

There is nothing built-in (AFAIK).

You could write an SBT plugin which inspects the .class files after compile task finishes (using a library like BCEL, ASM, etc.) and fails if it finds any value class constructor calls.

Alternately, you should be able to do the same with a compiler plugin (unfortunately, documentation I was able to find is quite old) with a little more difficulty.

like image 199
Alexey Romanov Avatar answered Nov 15 '22 10:11

Alexey Romanov