Generic Constraint: Subclass

generics

(Jon) #1

I am curious as to why this does not compile:

class SomeClass : BaseClass where T: BaseClass { ... }
class SomeDerivedClass : SomeClass

I get the following error: 'SomeClass' requires that 'SomeDerivedClass' inherit from 'BaseClass'

But doesn't it?


(Jordan Rose) #2

The definition of "T" is missing from your example, so it's hard to say.


(Jon) #4

Actually, lost the definition of T through lack of escaping and HTML I guess....Trying again.

class BaseClass {}
class SomeClass<T> : BaseClass where T: BaseClass {}
class SomeDerivedClass : SomeClass<SomeDerivedClass> {}

I get the following error: 'SomeClass' requires that 'SomeDerivedClass' inherit from 'BaseClass'


(Adrian Zubarev) #5

This looks like CRTP which is not yet supported in Swift. I don‘t remember if diagnostics were added (seems like from your posts), but it compiled before and simply stucked at the execution at some point.


(Jon) #6

CRTP is indeed what I was trying to get out of this...that tis a shame. Thanks!


(Adrian Zubarev) #7

Good news Swift 5 will allow CRTP. Thanks to @Slava_Pestov :beers: