I need to implement a macro that replaces an implementation of a class with a class of another implementation if the library does not exist. The reason is that users may not want to install a complex library (say, the name complex.jar). It is very important that the code compiles , even if the library is missing.
Practical example: Think for example. tool for calculating complex mathematical functions. Suppose now we want to add the functionality of constructing the result. For this we use Matlab (I know that there are others, this is just an example). Suppose Matlab has a jar file. Since not all users have Matlab, I want my code to compile without this jar, although it uses methods from this jar.
Example:
@ReplaceMeIfLibDoesNotExist("complex","DefaultConnector.scala")
class ComplexConnector{
import complex._;
def connect(){
complex = new ComplexLibrary(); // part of complex.jar
}
}
class DefaultConnector{
def connect(){
println("Currently not supported. Install complex.jar")
}
}
Will there be something like that? What will the macro implementation look like? Or is there an even more elegant way of doing such things? It would be nice to have a specific code example.
Thank you in advance.
source
share