Question Details

No question body available.

Tags

scala scala-3 opaque-types

Answers (4)

February 20, 2026 Score: 3 Rep: 1,604 Quality: Medium Completeness: 60%

Opaque types will also hide the constructors for SubClass1 and SubClass2. Consequently, the constructors must be defined within the companion objects WrappedSubClass1 and WrappedSubClass2.

Please find the complete working code provided below.

package thirdpartylibrary

abstract class BaseClass

final class SubClass1 extends BaseClass

final class SubClass2(val param: String) extends BaseClass
package opaquetypehierarchy

import thirdpartylibrary.{BaseClass, SubClass1, SubClass2}

opaque type WrappedBaseClass = BaseClass opaque type WrappedSubClass1
February 19, 2026 Score: 2 Rep: 23,043 Quality: Low Completeness: 10%

Have you checked if it works if you put them in the same file? If not, then I do not think you can make this work easily, and, TBH, I would just use normal wrapper classes, it is likely the overhead of the boxing is not that bad.

February 20, 2026 Score: 1 Rep: 8,386 Quality: Low Completeness: 0%

Thanks, @Yuriy!

I deliberately excluded constructors, etc. to keep this an MVE. I didn't realize the definitions had to go in the same file, and that fixed my issue.

February 20, 2026 Score: 0 Rep: 8,386 Quality: Low Completeness: 0%

Thanks @Luis! Doh! I wasn't aware that they needed to be in the same file! It looks like that's all I'm going to need...