Open som-snytt opened 1 year ago
I had not got around to trying it.
scala> implicit class xxx[A](val a: A) extends AnyVal { def chex(p: A => Boolean): a.type = {assert(p(a)); a} }
class xxx
scala> val sure: self.type = self.chex(_.length > 3)
^
error: type mismatch;
found : _1.a.type (with underlying type String) where val _1: xxx[String]
required: self.type
scala> val sure: self.type = xxx[self.type](self).chex(_.length > 3)
val sure: self.type = abcd
well,
scala> implicit class xxx[A <: Singleton](val a: A) extends AnyVal { def chex(p: A => Boolean): a.type = {assert(p(a));
a} }
class xxx
scala> val sure: self.type = self.chex(_.length > 3)
val sure: self.type = abcd
but
scala> implicit class xxx[A <: Singleton](private val a: A) extends AnyVal { def chex(p: A => Boolean): a.type = {assert
(p(a)); a} }
^
error: private value a escapes its defining scope as part of type xxx.this.a.type
can we change ensuring
?
Reproduction steps
Scala version: 2.13.11
Problem
ensuring
is not defined as returningself.type
and inference doesn't support the straightforward expression.Scala 3 is fine with it, but maybe is assisted by infra for new-style extensions:
(I'm not sure how it gets away with it, as I don't see
self.type
inferred anywhere, but I didn't research it.)