Closed
Description
Opaque types possessing black magic.
It appears that adding 3 or more opaque type numeric causes the code to fail to compile. However here, uncommenting in Line 15 makes the code compile, which is odd, as it is seemingly a random opaque type declaration. Adding 2 Amounts
is fine, the issue appears when adding 3 Amounts
Compiler version
3.1.1
Minimized code
trait Newtype[Src]:
opaque type Type = Src
transparent trait BigDecimalNewType extends Newtype[BigDecimal]:
override opaque type Type = BigDecimal
inline def apply(inline dec: BigDecimal): Type = dec
extension (self: Type)
inline def value: BigDecimal = self
inline def +(inline y: Type): Type = self.value + y.value
type Amount = Amount.Type
object Amount extends BigDecimalNewType {
// opaque type Blahblahblah = BigDecimal
}
case class Fees(
exchange: Amount,
slippage: Amount,
bribe: Amount
):
def calculateTotalFees: Amount = exchange + slippage + bribe
@main def main(): Unit =
val fees: Fees = Fees(Amount(1),Amount(2), Amount(2))
val totalFees: Amount = fees.calculateTotalFees
println(s"totalFees: ${totalFees}")
Scastie link: https://scastie.scala-lang.org/MLbciD4bQtysh7mVlYy1ig
Output
Found: (self$proxy1 : Any)
Required: BigDecimalNewType_this.Type
Expectation
Compile regardless of Line 15 in the code.