0

I have a project I'm working on in which we will eventually plan to open source portions of the project. Part of the project involves some IP that we don't want to open source. I would end up having two "packages" as part of the overall project

  • package Open - This is the open source portions
  • package IP - This is the closed source portions, and customers would pay to get this

Ideally I'd like to do something similar to the following

package Open

class MyOpenClass (val hasIP: Boolean = false){
  
  if(hasIP){
    import IP
    //do stuff with IP
  } else {
    //do stuff without IP
  }
}

This doesn't seem to work as IP is not found at compile time. I guess it's possible I could create a dummy IP package to be included with the Open project. That is a possibility, however I wanted to see if there was something I'm missing that can resolve this without the need of blackbox packages.

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
l Steveo l
  • 516
  • 3
  • 11
  • 1
    Can't you just check for a specific class that should exist? Something like [this](https://stackoverflow.com/a/3466596/13210997). – Filip Jul 18 '21 at 15:03
  • 1
    See https://stackoverflow.com/questions/2547867/checking-whether-a-package-is-existent-or-not – ccheneson Jul 18 '21 at 15:08
  • @ccheneson this seems to just catch if the package is not available? I will have the case where the package isn't available but I don't want the scala compiler to complain about it. – l Steveo l Jul 18 '21 at 15:23
  • 2
    The case where the package isn't available is handled by the `catch` block. If the package is not found, `Class.forName` will throw a `ClassNotFoundException`, caught by the `catch` block which returns false; – ccheneson Jul 18 '21 at 15:37
  • Logically, it should be the other way around where licenced part uses the open part. Other way can be to use class loaders (for example - how jdbc loads different driver profiles). – sarveshseri Jul 18 '21 at 18:55
  • @ccheneson I initially didn't search the more appropriate "scala conditional import" earlier. It appears that conditionally importing a package is not really possible. I will update an answer with my findings – l Steveo l Jul 18 '21 at 19:50

1 Answers1

0

Can we do conditional imports in Scala?

This is really what I was asking. Just did not use the proper term of "conditional" during searching.

This may also be another solution

Scala conditional compilation

The issue with the licensed part including the open part is that for this particular project, the licensed part is a subset of the open portion. Best example I can give is a car analogy where the open portion is the body and closed portion is the engine. By default, you get the open version engine which is slow, but if you pay for the licensed version you get a larger/more efficient engine.

l Steveo l
  • 516
  • 3
  • 11