package foo
private[foo] sealed trait Seal
private[foo] given Seal with {}
trait SealedWithinFoo(using Seal)
It’s not perfect, as it only precludes the definition of concrete types outside of foo. Still, it seems to guarantee that any subtype of SealedWithinFoo defined outside of foo cannot be inhabited.
Welcome to Scala 3.3.1 (17.0.8.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> object foo:
|
| private[foo] sealed trait Seal
| private[foo] given Seal with {}
|
| trait SealedWithinFoo(using Seal)
|
// defined object foo
scala> object outside extends foo.SealedWithinFoo
-- [E172] Type Error: ----------------------------------------------------------
1 |object outside extends foo.SealedWithinFoo
| ^
|No given instance of type foo.Seal was found for parameter x$1 of constructor SealedWithinFoo in trait SealedWithinFoo
1 error found
You don’t really need private[foo] on Sealed as it is sealed and thus can’t be instantiated outside foo, but good to have it as it prevents Sealed from being seen.