Self generic type

Sergey Metlov

I'm building the following class:

abstract class Foo
{
    protected abstract void Process(FooProcessor<T> processor)
}

What I need is to make T be the type of child Foo class:

class FooChild : Foo
{
    protected override void Process(FooProcessor<FooChild> processor)
    {
    }
}

Is it achievable? If so, how?

Adam Houldsworth

Something like a self-constraint will allow you to expose derived types and also constrain at the same time:

abstract class Foo<T> where T : Foo<T>
{
    protected abstract void Process(FooProcessor<T> processor);
}

Then derived types define themselves as the target:

class FooChild : Foo<FooChild>
{
    protected override void Process(FooProcessor<FooChild> processor)
    {
    }
}

Please note though that this design tends to only have a small set of uses. Also, you lose the ability to refer to Foo as a base without specifying it's generic type.

There is also a blog post from Eric Lippert about how it is possible to abuse this design, so you might want to consider what it is you are actually trying to achieve by wanting to have your protected method reference the derived class directly.

You may be better off using interfaces to encapsulate the behaviour you are trying to achieve.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use Self as generic type

From Dev

Scala trait with generic self type

From Dev

Self as argument type of generic callback

From Dev

Using self as a Generic Type Constraint

From Dev

Generate self-referencing generic type with JavaPoet

From Dev

Scala self-type and generic class

From Dev

Downcast Generic AnyObject to Protocol Associated Type Self.Model

From Dev

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

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

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

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

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

From Dev

self referencing generic with inheritence

From Dev

Typescript, self returning generic

From Dev

When attempting to instantiate an instance of a self-referential generic class, how you handle the self-reference type parameter

From Dev

java "generic generic type"

From Dev

Generic type in generic constraint

From Dev

generic as a generic type in Java?

From Dev

Generic type in generic constraint

From Java

Why does my self-bound generic type not match the method invocation?

From Java

What does "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements" mean?

From Dev

This generic type

From Dev

Generic Type of a Generic Type in Java?

From Dev

Self-referential Generic Types

From Dev

Self-contained generic memento

From Dev

generic collection generation with a generic type

From Dev

Typescript: generic that extends a type with a generic

Related Related

  1. 1

    Use Self as generic type

  2. 2

    Scala trait with generic self type

  3. 3

    Self as argument type of generic callback

  4. 4

    Using self as a Generic Type Constraint

  5. 5

    Generate self-referencing generic type with JavaPoet

  6. 6

    Scala self-type and generic class

  7. 7

    Downcast Generic AnyObject to Protocol Associated Type Self.Model

  8. 8

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

  9. 9

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

  10. 10

    Self referencing generic type constraint and XAML in UWP application

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

    self referencing generic with inheritence

  16. 16

    Typescript, self returning generic

  17. 17

    When attempting to instantiate an instance of a self-referential generic class, how you handle the self-reference type parameter

  18. 18

    java "generic generic type"

  19. 19

    Generic type in generic constraint

  20. 20

    generic as a generic type in Java?

  21. 21

    Generic type in generic constraint

  22. 22

    Why does my self-bound generic type not match the method invocation?

  23. 23

    What does "Protocol ... can only be used as a generic constraint because it has Self or associated type requirements" mean?

  24. 24

    This generic type

  25. 25

    Generic Type of a Generic Type in Java?

  26. 26

    Self-referential Generic Types

  27. 27

    Self-contained generic memento

  28. 28

    generic collection generation with a generic type

  29. 29

    Typescript: generic that extends a type with a generic

HotTag

Archive