Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to implement a Macro which replaces the implementation of a class by a class of another implementation, if a library does not exist. The reason is that users might not be willing to install a complex library (say the name is complex.jar). It is very important that the code compiles even if the library is not present.

Practical Example: Think of e.g. a tool for computing complex mathematical functions. Let us assume that we now want to add the functionality of plotting the result. For this we use Matlab (I know there are others, this is just an example). Let us assume that Matlab has a jar file. Since not all users have Matlab installed, I want that my code compiles also 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")
   }
}

Would something like that work? How would the Macro implementation look like? Or is there some more elegant way of doing things like that? It would be nice to have a concrete code example.

Thank you very much in advance.

share|improve this question
1  
I am very much a fan of Scala's macro system, but this is terrifying. –  Travis Brown Mar 27 at 22:48
    
What do you mean by terrifying? Is this too difficult? Or bad style? What do you do in such a case? Can you do something better without Macros? –  user1729603 Mar 28 at 10:06
    
It sounds like you're essentially bullying the compiler into saying yes to something that you and it both know may blow up down the line. It feels like a throwback to the bad old days of configuring your Java program with class names read at runtime from strings. –  Travis Brown Mar 28 at 13:28
    
The idea is that this will be part of a larger project where the library 'complex' is not neccessary to do the main work with the tool. However, if users want to increase speed for a specific task, they may load one library. This library has a quite complex installation process which not every user should be forced to do (especially if he does not need the functionality). I tried to made it clearer above including an example. –  user1729603 Mar 31 at 16:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.