Use Self as generic type

Rengers

Self can be used as the return type of a method:

func doSomething() -> Self {}

Is it somehow possible to use Self as a generic type like this?

func doSomething() -> Wrapper<Self> {}

Example

It would be nice if I could subclass ChristmasPresent and let it have a wrapped function that returns a WrappedPresent with the generic set to whatever the subclass was.

class ChristmasPresent {
    func wrapped() -> WrappedPresent<Self> {
        return WrappedPresent(present: self)
    }
}

class WrappedPresent<T: ChristmasPresent> {
    var present: T

    init(present: T) {
        self.present = present
    }
}

class ToyCar: ChristmasPresent {}

let wrappedToyCar = ToyCar().wrapped() // Inferred to be: WrappedPresent<ToyCar> 
Rob Napier

The most vexing paradox in Swift is this: "Swift prefers methods, but Swift's functions are more powerful." The Swift team knows that, and someday I am certain we will have powerful methods. But today is not that day. There are many things you'd like to express in methods that you cannot. Everything you want can be done easily with functions, however.

class ChristmasPresent {}

struct WrappedPresent<T: ChristmasPresent> {
    let present: T
}

func wrap<T:ChristmasPresent>(present: T) -> WrappedPresent<T> {
    return WrappedPresent(present: present);
}

class ToyCar: ChristmasPresent {}

let wrappedToyCar = wrap(ToyCar()) // Inferred to be: WrappedPresent<ToyCar>

Note that if your code did compile, you might still be quite surprised at the result. Swift custom types are not covariant, so a WrappedPresent<ToyCar> is not a subtype of WrappedPresent<ChristmasPresent>. So if you had an array of wrapped presents, you could not put a wrapped toycar in it. That could easily force you back to just using a fixed (non-type-parameterized) WrappedPresent type anyway, making the question moot. Generics and classes do not always mix as well as you might imagine in Swift.

If you have a practical example of a problem you'd want to solve with this, then I do recommend bringing it up on the dev forums. The Swift team is very responsive.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to use generic protocol as a variable type

From Dev

Use Generic Method With Type DataType

From Dev

Class and generic type casting to use in parameterised type

From Dev

Self-type annotation, why to use self: => and not val?

From Dev

Self generic type

From Dev

Use == operator with generic type in a Where Linq statement

From Dev

How to use a helper generic of type class of type

From Dev

Generic type use, in Java

From Dev

Use type member as self-type

From Dev

Use class instance as generic type

From Dev

Generate self-referencing generic type with JavaPoet

From Dev

Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

From Dev

Self referencing generic type constraint and XAML in UWP application

From Dev

Scala trait with generic self type

From Dev

Downcast Generic AnyObject to Protocol Associated Type Self.Model

From Dev

How to use generic type in a map?

From Dev

Protocol with property of type Self can only be used as generic constraint, why?

From Dev

Use anonymous type in generic class

From Dev

Classmethods in Generic Protocols with self-types, mypy type checking failure

From Dev

Use Generic Method With Type DataType

From Dev

Self as argument type of generic callback

From Dev

Define generic type for use in closure

From Dev

Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

From Dev

Protocol with property of type Self can only be used as generic constraint, why?

From Dev

Use PropertyInfo as generic type when calling a generic method

From Dev

Use type of self as function parameter in extension

From Dev

Using self as a Generic Type Constraint

From Dev

Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

From Dev

Scala self-type and generic class

Related Related

  1. 1

    How to use generic protocol as a variable type

  2. 2

    Use Generic Method With Type DataType

  3. 3

    Class and generic type casting to use in parameterised type

  4. 4

    Self-type annotation, why to use self: => and not val?

  5. 5

    Self generic type

  6. 6

    Use == operator with generic type in a Where Linq statement

  7. 7

    How to use a helper generic of type class of type

  8. 8

    Generic type use, in Java

  9. 9

    Use type member as self-type

  10. 10

    Use class instance as generic type

  11. 11

    Generate self-referencing generic type with JavaPoet

  12. 12

    Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

  13. 13

    Self referencing generic type constraint and XAML in UWP application

  14. 14

    Scala trait with generic self type

  15. 15

    Downcast Generic AnyObject to Protocol Associated Type Self.Model

  16. 16

    How to use generic type in a map?

  17. 17

    Protocol with property of type Self can only be used as generic constraint, why?

  18. 18

    Use anonymous type in generic class

  19. 19

    Classmethods in Generic Protocols with self-types, mypy type checking failure

  20. 20

    Use Generic Method With Type DataType

  21. 21

    Self as argument type of generic callback

  22. 22

    Define generic type for use in closure

  23. 23

    Extending a Protocol where Self: Generic Type in Swift (Requires Arguments In <...>)

  24. 24

    Protocol with property of type Self can only be used as generic constraint, why?

  25. 25

    Use PropertyInfo as generic type when calling a generic method

  26. 26

    Use type of self as function parameter in extension

  27. 27

    Using self as a Generic Type Constraint

  28. 28

    Incorrectly Inferred Type in Generic Closure with Protocol Extension Constrained by Self

  29. 29

    Scala self-type and generic class

HotTag

Archive