Home > Software engineering >  Making member package protected using wildcards
Making member package protected using wildcards

Time:08-26

I have the following situation:

package com.my.organisation.common.gateway

object Singleton {
  val entrypoint = Entrypoint()
}

I would like to make entrypoint available to classes within com.my.organisation.common.gateway but also within com.my.organisation.other.gateway and also other future com.my.organisation.*.gateway packages.

Is there a way of doing this in Scala? protected val[com.my.organisation.*.gateway] does not compile, but that's the behaviour that I am aiming for.

CodePudding user response:

try this.

package com.my.organisation.common.gateway

object Singleton {
  private[organisation] val entrypoint = Entrypoint()
}

see also: https://alvinalexander.com/scala/how-to-control-scala-method-scope-object-private-package/

CodePudding user response:

I suggest using a package object declared in the most common path of the 2 packages. Then this can be shared to all sub-packages, because package objects acts as convenient containers shared across an entire package.

Assuming the code for Entrypoint is defined somewhere in the earlier packages.

// in file com/my/organisation/package.scala
package com.my
package object organisation {
   val entrypoint: Entrypoint = Entrypoint()
}

Then you can import it with import com.my.organisation._ wherever you need it.

  • Related