Guarantees of `this.type`

So, I must ask: Is the implicit-based variant ok? I.e.

sealed trait Foo
implicit class FooExt[T<:Foo](val x:T) extends AnyVal{
    def foo:T = unsafeImplementation(x).asInstanceOf[T]
}
def unsafeImplementation(x:Any):Any = ???
...

Something fishy must happen in the implementation, but is this an acceptable way of publishing an interface that “returns the same type”?

I mean, there are cases where this could run into the same issue: e.g. when called on this from within a class extending Foo. then T is this.type and we violate the type system; probably other issues if one has non-static inner classes that extend Foo (something something path dependent types). But these are avoidable by carefully controlling all classes that implement Foo (it’s sealed).

Well I’m not sure I really understand what you’re trying to do so I’m not going to attempt a full answer, but: sealed does in fact guarantee that nobody else can subclass Foo (at least without abusing reflection or bytecode manipulation, and it isn’t your job to guard against that). And since you control this little closed universe, I can imagine that gives you options you wouldn’t have otherwise.

Perhaps someone else would like to chime in with further thoughts.