0

Is it possible to inherit (and override) type aliases in Swift classes? I tried the following and it did not work.

http://swiftstub.com/443158944/

I took a shot in the dark and tried "override" but it's a no go.

Is it at all possible? If it's not possible is there a workaround? (like returning a specific type from a function or something crazy)

thekwiat
  • 314
  • 3
  • 9
  • 2
    A workaround to _what_? What real thing are you really trying to do? – matt Jan 10 '15 at 03:25
  • Well, I was hoping to allow subclasses to state their type (i.e. with a typealias that they would inherit from a base class and override). I wanted to avoid using "is" or "as?" which would require me to iterate through the types of all the different possible subclasses. – thekwiat Jan 10 '15 at 03:43
  • If you must know, this is part of a complicated attempt to downcast to a protocol when I can't use @objc (the protocol has an enum). – thekwiat Jan 10 '15 at 03:44
  • Can you give an example? "State their type" how or in what way? Subclasses are polymorphic; they do not need to "state their type" because they do the right thing for their type automatically. And protocol is not a class, and enum is not a class, so I don't see how that remark explains your purpose. – matt Jan 10 '15 at 03:49
  • You can't use the @objc tag when a protocol contains an enum. See: http://stackoverflow.com/questions/24140545/how-to-pass-swift-enum-with-objc-tag And the issue of downcasting to a protocol in this case is well known. See: http://stackoverflow.com/questions/24299635/downcast-from-any-to-a-protocol and: http://iphonedevelopmentexperiences.blogspot.com/2014/06/cannot-downcast-from-anyobject-to-non.html – thekwiat Jan 10 '15 at 04:18
  • Well, it was a little complicated so I left it out of the original question. The subclasses are represented polymorphically (in an array with type [Superclass]) and I wanted to cast them to their actual Subclass types, when in theory the compiler would know exactly which ones conform to the protocol in question – thekwiat Jan 10 '15 at 04:22
  • If overriding a typealias is not possible, I would just like to know...And then others could actually find something definitive when they google search this question. – thekwiat Jan 10 '15 at 04:25
  • Whoah man, I definitely wanted help. Just because my blind guess didn't work doesn't mean I wasn't missing something simple. – thekwiat Jan 10 '15 at 04:31
  • If you want help show your code and desired outcome. Show an actual _problem_ you are trying to solve. This is the third time I have asked you. – matt Jan 10 '15 at 04:37
  • Here is a swift stub: http://swiftstub.com/643919990/ No sweat if you don't want to spend time on this...It's just a goofy idea. – thekwiat Jan 10 '15 at 04:55
  • Very interesting. I don't understand why you don't just delete this question and use that code as the basis of your question. That's a good question. This question is not. – matt Jan 10 '15 at 05:16
  • I still don't quite understand why you even need the protocol. You have a set of classes. You can give your superclass the function. The others inherit it. Then you just call it. If you implement it correctly, it will do the "right thing" no matter what the dynamic type of this instance is (polymorphism). I don't see what else you need. It can be a no-op for some classes if you want it to be. – matt Jan 10 '15 at 05:23
  • Right, in my actual class hierarchy the base class is something generic like a "TreeNode" and there are a series of unrelated protocols that my subclasses of "TreeNode" may or may not conform to. The protocols are things like "WillRedrawEveryFrame" in which case the subclasses would have a redraw() function. So it seemed that "TreeNode" shouldn't have a function like redraw() for subclasses to override. It was a kinda tricky design situation. I guess I should just make another question then, thanks. – thekwiat Jan 10 '15 at 05:57

0 Answers0