Thanks for your response! Some comments:
I realize that
self will return an instance of B. However, any instance of B must also be a valid instance of A, as it was successfully assigned to
a. In this case, the result of
a.self() can continue to be used as A, even if the actual type is B. For the example above, the code can be translated to the following:
val a: A = B()
(a.self() as A)
The intent is not to have
a.self() suddenly be useable as B but rather continue to be A. (Perhaps “self type” is not the correct terminology here? I’m not familiar with how self types work in other languages, so I may be misusing the term).
The use case I had in mind for this makes extension functions a less desirable alternative, as it’s little better from overriding the method in every sub-class to update the return type.
I agree that such an example would require proper self types to function, and I recognize that this is something that is being considered. My proposal above would not support such a use case, as it only supports methods returning
This by itself.
Overall, my goal is to explore whether a subset of self type functionality can be easily implemented in a way that is compatible with a future full implementation. Given the challenges with self types on the JVM, it doesn’t seem like true self types are coming anytime soon. Is there a way to obtain some of the benefits now?