how to say "same class" in a Swift generic -


if swift generic type constraint protocol name, can require 2 types, constrained protocol, same type. example:

protocol flier {} struct bird : flier {} struct insect: flier {} func flocktwotogether<t:flier>(f1:t, f2:t) {} 

the function flocktwotogether can called bird , bird or insect , insect, not bird , insect. limitation want. far, good.

however, if try same thing class name, doesn't work:

class dog {} class noisydog : dog {} class wellbehaveddog: dog {} func walktwotogether<t:dog>(d1:t, d2:t) {} 

the problem can call walktwotogether wellbehaveddog , noisydog. want prevent.

there 2 questions here:

  • is there way walktwotogether can't called wellbehaveddog , noisydog?

  • is bug? ask because if can't use generic this, hard see why useful generic constraint class name @ all, since same result normal function.

not answer, per se, more data perhaps... problem when call:

walktwotogether(noisydog(), wellbehaveddog()) 

swift can treat both instances if they're instances of dog (aka, upcast) — need can call methods meant class a subclasses of a. (i know know this.)

swift doesn't upcast protocols, way specify protocol subclasses superclass doesn't conform to:

protocol walkable {} extension noisydog : walkable {} extension wellbehaveddog: walkable {} func walktwotogether<t: dog t: walkable>(d1:t, d2:t) { }  walktwotogether(noisydog(), wellbehaveddog()) // error: type 'dog' not conform protocol 'walkable' 

the error message explicitly shows going on — way call version of walktotogether upcast subclass instances dog, dog doesn't conform walkable.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -