-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opaque type weirdness: (self$proxy1 : Any) Required: BigDecimalNewType_this.Type #14656
Comments
I'm wondering if what you're trying to do here should be possible at all #14659 |
I don't see why it should not be possible? Although your example (opaque alias of But regardless this current issue still persist even if you avoid override of opaque type: trait Newtype[Src]:
opaque type Type = Src
transparent trait BigDecimalNewType extends Newtype[BigDecimal]:
inline def apply(inline dec: BigDecimal): Type = dec.asInstanceOf[Type]
extension (self: Type)
inline def value: BigDecimal = self.asInstanceOf[BigDecimal]
inline def +(inline y: Type): Type = (self.value + y.value).asInstanceOf[Type]
type Amount = Amount.Type
object Amount extends BigDecimalNewType {
// Uncommenting this fixes the issue.
// opaque type Blahblahblah = BigDecimal
}
case class Fees(
exchange: Amount,
slippage: Amount,
bribe: Amount
):
def calculateTotalFees: Amount = exchange + slippage + bribe |
Ok, so that's a separate issue though it might have a similar genesis to #14653. trait BigDecimalNewType:
opaque type Type = BigDecimal
def apply(value: BigDecimal): Type = value
extension (self: Type)
def value: BigDecimal = self
inline def +(y: Type): Type = apply(self.value + y.value)
object Amount extends BigDecimalNewType {
val x = Amount(0)
val y = (x + x) + x
} but here defining an additional unused opaque type doesn't make everything magically compile |
Yeah weird. Adding |
That particular problem has just been fixed #14665 |
Reduced further to: trait BigDecimalNewType:
opaque type Type = BigDecimal
def apply(value: BigDecimal): Type = value
def value(self: Type): BigDecimal = self
inline def plus(self: Type, y: Type): Type = apply(value(self) + value(y))
object Amount extends BigDecimalNewType {
val x = Amount(0)
val y0 = plus(x, x)
val y = plus(y0, plus(x, x))
} Problem goes away if object BigDecimalNewType:
opaque type Type = BigDecimal
def apply(value: BigDecimal): Type = value
def value(self: Type): BigDecimal = self
transparent inline def plus(self: Type, y: Type): Type = apply(value(self) + value(y))
object Amount {
import BigDecimalNewType.*
val x = BigDecimalNewType(0)
val y0 = plus(x, x)
val y = plus(y0, plus(x, x))
} |
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 3Amounts
Compiler version
3.1.1
Minimized code
Scastie link: https://scastie.scala-lang.org/MLbciD4bQtysh7mVlYy1ig
Output
Expectation
Compile regardless of Line 15 in the code.
The text was updated successfully, but these errors were encountered: