Search Unity

Question Is it possible to do arithmetic with generic types?

Discussion in 'Scripting' started by ZeHgS, May 15, 2022.

  1. ZeHgS

    ZeHgS

    Joined:
    Jun 20, 2015
    Posts:
    117
    Hello!

    I wanted to replace the floats in the class below with a generic type so that I could use either floats or ints with it. I am a beginner at C# so I was pretty overwhelmed by the search results I found when googling it, particularly since Unity doesn't use the most up-to-date version of C# or .NET so I am unsure what applies and what doesn't. Is it possible to do so and still perform the arithmetic operations below?

    Thank you all very much!

    Code (CSharp):
    1. public class Stat<T> where T : System.Enum
    2. {
    3.     public                            T                attributeName;
    4.  
    5.     [SerializeField]    protected    float            baseValue;
    6.  
    7. // ***************************** HERE IS THE ARITHMETIC *********************** //
    8. // ***************************** HERE IS THE ARITHMETIC *********************** //
    9. // ***************************** HERE IS THE ARITHMETIC *********************** //
    10.     protected            float                        currentBaseValue
    11.     {
    12.         get {
    13.                 return baseValue * (1 + (increasePerLevel * (currentLevel - 1)));
    14.                }
    15.     }
    16. // ***************************** END ARITHMETIC *********************** //
    17. // ***************************** END ARITHMETIC *********************** //
    18. // ***************************** END ARITHMETIC *********************** //
    19.  
    20.  
    21.     [SerializeField]    private        int                currentLevel        = 1;
    22.  
    23.     [SerializeField]    protected    float            increasePerLevel    = 0.25f;
    24. }
     
    Last edited: May 15, 2022
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,987
    No, that's currently not possible. The latest version of C# itself has a preview feature for this by providing specific interfaces for different operator implementations.

    Apart from that, what would be your concrete usecase? Keep in mind that generics are essentially the opposite of inheritance / polymorphism. Two concrete generic types (so when the generic argument is bound to an actual type) are not compatible with each other in any way. The point of polymorphism is to share the same data but be able to replace the functionality. So two objects are the same thing but can have different behaviour / functionality. Generics do the opposite. They share the exact same functionality but represent totally different objects / data.

    So for example you can not have a "Stats" array and put different concrete generic types in it. Generic types are not in any way compatible with each other (with the narrow exception of covariance / contravariance, but that only applies to rare cases where the data flow is one-directional which it is not in your case).
     
    ZeHgS likes this.
  3. ZeHgS

    ZeHgS

    Joined:
    Jun 20, 2015
    Posts:
    117
    Thank you very much for replying!

    That's too bad! That's actually my use case. I have certain stats like movement speed that are floats and others like number of bullets that are ints. I'll just add a bool to treat them as ints/floats or something, then. Thanks for all the info!