Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 467 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limitation with classes derived from generic classes in swift

#1
I'm trying to derive my class from generic class:

class foo<T> {}
class bar : foo<Int> {}

But this code fails to compile with en error:

> Classes derived from generic classes must also be generic

How to avoid this limitation? Is it possible?
Reply

#2
This is now officially supported:
```
class Foo<RelatedType> {
var object: RelatedType? = nil

func myFunction(param: RelatedType) -> RelatedType? {
return nil
}
}

class Bar: Foo<String> {
override func myFunction(param: String) -> String? {
return self.object
}
}
```
Reply

#3
In Swift 2 it's possible to subclass a generic class:

class Something {}

class Base<T> {}

class Fancy : Base<Something> {}
Reply

#4
I had the same issue and I found a solution!

class SomeType {
}
class Foo<T> {
}
class Bar<T: SomeType>: Foo<T> {
}

Now you can do:

let bar = Bar()

This will default Bar's type to SomeType. The only drawback is that you can only do this for 'subclassable' types. So for `Int` this will not work.
Reply

#5
An alternative to the `typealias` [solution](

[To see links please register here]

) is to take the documentation literally:

> You can subclass a generic class, but the subclass must also be a
> generic class.

So we make subclass a generic class, but of another type that we don't use for anything.

class foo<T> {

func doSomething(t:T) -> T {
return t;
}
}

class bar<S> : foo<Int> {

override func doSomething(t:Int) -> Int {
return t * t;
}
}

let b : bar = bar<AnyObject>()
b.doSomething(5) // 25 instead of 5


I prefer this approach to using `typealias` because it doesn't pollute the namespace with an additional type. This is of particular importance if the subclass (`bar`) is meant to be `public` or `internal`.

**Edit**

The documentation no longer includes the phrase about the limitation of generic subclasses. Hopefully this means that Apple plans to change this sooner than later.
Reply

#6
Ssreg,

Unfortunately this is [official][1]:

> You can subclass a generic class, but the subclass must also be a
> generic class.

Let us hope Apple fixes this in a future version.

Meanwhile, let us see this as an opportunity to exploit [aggregation][2] instead of subclassing.

NOTE:

As a poor man's version of a solution, one could use `typealias`:

class foo<T> {}
class bar<Int> : foo<Int> {}
typealias Bar = bar<Int>

This way, the rest of the code can be written just as if Apple already fixed the matter.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through