Action as generic type in constructor gives compile error

mungflesh

Why does the constructor overload in Funky<T> have an issue with the action parameter but the subclass FunkyAction does not?

class Funky<T>
{
    readonly T _data;

    public Funky(T data)
    {
        _data = data;
    }

    public Funky(Action action, bool imJustAnOverload)
        : this(action) // cannot convert from 'System.Action' to 'T'
    {
    }
}   


class FunkyAction : Funky<Action>
{
    public FunkyAction(Action action)
        : base(action) // no compile error
    {
    }
}
Yacoub Massad

This constructor:

public Funky(Action action, bool imJustAnOverload)
    : this(action)

is trying to use this constructor:

public Funky(T data)

passing an Action as a argument to a parameter of type T.

Since T is a generic type paremeter, then the compiler cannot guarantee that action can be casted to T. As far as the compiler is concerned, T could be an int or a string.

Now, for the derived class, this constructor:

public FunkyAction(Action action)
    : base(action)

Is trying to use this base class constructor:

 public Funky(T action)

But since it is defining T as Action (in class FunkyAction : Funky<Action>), then the base constructor actually looks like this (from the perspective of FunkyAction in specific):

public Funky(Action action)

Now there is no problem passing an argument of type Action to a method that expects an Action.

You could make the base class constructor generic like this:

public Funky(T action, bool imJustAnOverload)
        : this(action)
    {
    }

That will allow you to create a Funky<Action> and construct it with an Action like this:

Funky<Action> funky = new Funky<Action>(() => DoSomething(), true);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

std::thread initialization constructor gives compile error

From Dev

std::thread initialization constructor gives compile error

From Dev

Inferring a generic type from a generic type in Java (compile time error)

From Dev

Nested generic with type bound results in compile error

From Dev

"Expected type parameter" error in the constructor of a generic struct

From Dev

Calling constructor of generic type?

From Dev

Generic type as a constructor parameter

From Dev

Type safe generic Java observer compile time error

From Dev

Type mismatch compile error trying to apply a lambda expression in a generic class

From Dev

Java Compile error when return Generic Class type

From Dev

Generic type within Action?

From Dev

Generic type within Action?

From Dev

Explicit type annotation for generic constructor of a generic type

From Dev

Why does giving explicit type arguments to a non-generic method or constructor compile?

From Dev

Metal iOS gives compile error

From Dev

comparing and thenComparing gives compile error

From Dev

VBA sub call gives "compile error: Type mismatch: array or user-defined type expected"

From Dev

Generic return type - unable to compile

From Dev

How to implement a constructor for a generic type

From Dev

Overloading constructor with a generic type in java

From Dev

Generic constructor with new type constraint

From Dev

How to make a constructor of a generic type

From Dev

Generic constructor allow int type

From Dev

Generic constructor allow int type

From Dev

Generic repository with generic constructor of type T

From Dev

explicit copy constructor compile error

From Dev

Compile time error and Constructor "Super"

From Dev

Compile time error and Constructor "Super"

From Dev

Java Compile Error - No Suitable Constructor

Related Related

  1. 1

    std::thread initialization constructor gives compile error

  2. 2

    std::thread initialization constructor gives compile error

  3. 3

    Inferring a generic type from a generic type in Java (compile time error)

  4. 4

    Nested generic with type bound results in compile error

  5. 5

    "Expected type parameter" error in the constructor of a generic struct

  6. 6

    Calling constructor of generic type?

  7. 7

    Generic type as a constructor parameter

  8. 8

    Type safe generic Java observer compile time error

  9. 9

    Type mismatch compile error trying to apply a lambda expression in a generic class

  10. 10

    Java Compile error when return Generic Class type

  11. 11

    Generic type within Action?

  12. 12

    Generic type within Action?

  13. 13

    Explicit type annotation for generic constructor of a generic type

  14. 14

    Why does giving explicit type arguments to a non-generic method or constructor compile?

  15. 15

    Metal iOS gives compile error

  16. 16

    comparing and thenComparing gives compile error

  17. 17

    VBA sub call gives "compile error: Type mismatch: array or user-defined type expected"

  18. 18

    Generic return type - unable to compile

  19. 19

    How to implement a constructor for a generic type

  20. 20

    Overloading constructor with a generic type in java

  21. 21

    Generic constructor with new type constraint

  22. 22

    How to make a constructor of a generic type

  23. 23

    Generic constructor allow int type

  24. 24

    Generic constructor allow int type

  25. 25

    Generic repository with generic constructor of type T

  26. 26

    explicit copy constructor compile error

  27. 27

    Compile time error and Constructor "Super"

  28. 28

    Compile time error and Constructor "Super"

  29. 29

    Java Compile Error - No Suitable Constructor

HotTag

Archive