Self() in final class

Currently, this doesn't work:

final class Foo {
	let bar = Self()
	//	Error: Covariant 'Self' type cannot be referenced from a stored property initializer
	//	Error: Stored property cannot have covariant 'Self' type
}

Should the compiler allow Self() in final classes?

4 Likes

This has been filed as SR-11176 and SR-11414 (among others). I'm not sure whether this could be considered a bug fix or if it would need to go through the evolution process.

4 Likes

It looks like it doesn't needs to go through evolution. From SE-0068 decision notes:

The core team has accepted the first half for this proposal. This allows the use of “Self” as shorthand for referring to the containing type (in the case of structs, enums, and final class) or the dynamic type (in the case of non-final classes)

I have an old PR that was supposed to fix this, but I forgot about it. I'll try to get back to it when I have some more free time (but feel free to take over my work if you want!).

7 Likes
Terms of Service

Privacy Policy

Cookie Policy