Scala Prohibit allocation of value classes - scala

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?

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.

Related

Is there a way to get a warning when a Scala Value Class needs to become instantiated?

In the documentation about Scala value classes, it is mentioned that there are three cases when a value class needs to actually be allocated an instance:
Allocation Summary
A value class is actually instantiated when:
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 a setting in the compiler or in the language features which would produce a warning when a value class needs to be instantiated?
No, not currently.
However, it is very rarely worth bothering with this kind of micro-optimisation.
If you have some very very hot code and you need to optimise it as far as possible then just try a few things and re-benchmark.
The JIT compiler will change what your code is doing at the machine level a lot of the time if the code is hot enough.
The overhead of allocating a value class is often not even measurable unless it is the only thing the thread is doing. See e.g. https://groups.google.com/forum/#!topic/scala-user/XdQnbcs2SRM for some benchmarks where value class allocation is not measurable.

Scala: Difference between file.class and file$.class from scalac

When I use scalac to compile file.scala, I end up with 2 outputs, file.class and file$.class. What is the difference between these files and which is the appropriate one to then run? I get distinctly different error messages between executing "scala file" vs "scala file$".
Scala objects get compiled to classes ending in "$" because you're allowed to have an "ordinary" class with the same name. But the object's methods are also exposed as static methods on the "ordinary" class, so that they can be called under the names you would expect. This is an artifact of trying to represent the scala semantics in a way that make sense to Java / the JVM, and I would encourage you to regard it as an implementation detail rather than something important.
(#MattPutnam's answer is correct that anonymous classes, including closures, are compiled to class files with $es in their name, but that's not what's causing your file$.class in this particular instance)
Use scala file. If you're interested in the implementation details you might also want to try java -cp /path/to/scala-library.jar file.
file$.class is some inner anonymous class. In Java they're very explicit, but they can be easy to miss in Scala. If you use any method that takes a function, there's an implicit anonymous class there. Post the code and I'll point it out.

Scala: Lazy baking and runtime compilation of cake pattern

One of the great limitations of the cake pattern is that its static. I would like to be able to mix-in traits potentially written by different coders completely independently. However the traits would not need to be mixed-in frequently. The user would have an initialisation screen where they would choose the traits / assemblies, before the main application was run. So the thought occurred to me why not mix-in and compile the chosen traits from with in the user choice selection module. If the compilation failed, no problem the user would just get back some message - incompatible assemblies or what ever. If the compilation succeeded then the top UI module would load the newly compiled classes with the pre-compiled parts of the assemblies and run the main application. Note there might only need to be one or two classes compiled duruing run time initialisation. All the rest of the code could have been compiled normally.
I'm pretty new to Scala. Is this a recognised pattern? Is there any support for it? It seems mad to have to use Guice for a relative simple dependency situation. Can I run the Scala compiler easily from within an application? Can I run it in memory and its outputs be used from memory without unnecessary file creation?
Note: Although appearing to be dynamic, this methodology would remain 100% static.
Edit it occurs to that one of the drives of Microsoft's Roslyn project was to enable just this sort of thing for C# and Visual Basic. But that seems to have been a pretty big project even for a high powered Microsoft team.
Calling the compiler directly from within Scala is doable, but not for the timid. Luckily, the good people at Twitter have automated the process for you. (140 character celebrity micro-blogging, and some cool Scala utilities! Thanks Twitter.) You can use the com.twitter.utils.Eval class to compile and evaluate Scala strings. In your example, you would do something like
val eval = new Eval()
val myObj = eval[BaseClass]("new BaseClass extends " + traitNameList.mkString(" with "))
This will create you a new object with all of the traits you desire built in. The question then arises as to whether this is a good idea. Downsides:
Calling out to the Scala compiler is not quick
If you do this enough, you will overload the PermGen space, as the classes you create will never be garbage collected
This really is more of the sort of thing you want a dynamic language for rather than Scala. You're likely to find places where this all kinds of works, but clashes with the rest of your architecture (yes, that's vague).

class file optimization with the scala compiler

I have some class file inside a jar. Now I am searching for a way to optimize that class file using some components(most notably the Inliners) of the scala compiler.
My idea is to :
use the ICodeReader to emit ICode from class file
use an instance of the Inliner class in order to achieve the desired optimization
I am not know if that´s the right way to go
The problem is that
How to use the ICodeReader in order to read a class file and produce the needed ICode. ICodeReader inherits from ClassfileParser. The sole method that is for me more probable to use is parse(file: AbstractFile, root: Symbol) but the problem the root argument.
Any help is welcome
The Scala compiler acts on the source code and on the results of the intermediate step to produce bytecode, and will try to apply, if enabled, several optimisation.
In your case, if I understood right, you do not have the sources but the compiled classes, and I would not expect you being able to use a feature of Scala compiler on compiled classes.
What you should be looking to, in my understanding, is a bytecode optimizer , such as ProGuard, which is able to take existing bytecode and optimize it without having access to the source code
http://proguard.sourceforge.net/#FAQ.html

GWT Generators - Determine Whether a Class is Referenced Anywhere

I have GWT project that uses Generators to create light dynamic reflection objects.
I was wondering if anybody knows of a way to determine whether or not a particular class is referenced in the dependency tree beginning at all EntryPoints. If I could do this, I could avoid generating reflection data for classes that will never be used anyway.
My understanding is that when GWT does its compiling, it performs a similar check so that it can reduce the total size of the compiled code, but I haven't been able to find any related methods in TypeOracle or anything like that.
This is an indirect method of accomplishing what you are getting at. I believe each GWT module, is fully packaged into a regular java package. You can use
TypeOracle.findPackage(String pkgName)
to get the JPackage instance, and on that instance you use findType(String typeName) to see if a type is present in that package. If present, its likely that it is referenced in some file and GWT will compile it.
There is also this method getPackages() which returns an array of all packages known to this type oracle - therefore reachable for GWT compiler.
JPackage[] getPackages()
You can iteratively findType() on each package to find if the type is going to be compiled or not.
The BEST method is to define a custom annotation and whitelist all the classes that you do want to generate reflection code. You can annotate the required classes with it, and checking for that presence of annotation before generating code for it.
My favorite is to follow a naming convention over annotation, (I did both together), and thus maintain a whitelist, and make the convention (its usually a REGEX) a "setting" that can be changed however the team wants.