Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Generic class Constraint with base class syntax confusion.

Discussion in 'Scripting' started by Krellumdaed, Jan 29, 2019.

  1. Krellumdaed

    Krellumdaed

    Joined:
    Jun 24, 2015
    Posts:
    22
    I would like to have my own ScriptableObject base class that provides some additions to the Scriptablebject class. The idea I am trying to implement seems like it would have this syntax but it does not compile.
    Code (CSharp):
    1. public class MySoBase<T> where T : MySoBase : ScriptableObject
    2. {
    3.    T MainSoField;
    4.    // ....
    5. }
    6.  
    . The idea being that only T' can be used that are children of MySoBase which is a child of Scriptable object.

    I would use it like this:
    Code (CSharp):
    1. public class MySo : MySoBase<MySo>
    2. {
    3.   int x;
    4.   MainSoField.x = 1;
    5. }
    Reading the MS docs on generic constraints (and other documentation) it almost seems like the constraint on T gets treated like inheritance rather than just as a constraint. Thus the compiler thinks I am trying to do multiple-inheritance and assumes I will have Dimond problem. But, if MySoBase as just a constraint, not an inheritance this should work.

    Am I missing something or is the C# syntax just limited - yet again? I came from a C/C++ deep background and the syntax messes I getting to with C# are really annoying.
     
  2. Krellumdaed

    Krellumdaed

    Joined:
    Jun 24, 2015
    Posts:
    22
    Sorry, I don't have the example correct. I have tried to change it but the system will not let me. The example I gave is a little pointless since the X is available without the field. But the question is still the same: Is there a syntax that allows me to constrain a class generic parameter without it being treated as a base class so I can still inherit from a base class.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    First off your constraint should go after what you're inheriting from... like so:

    Code (csharp):
    1.  
    2.     public class MySoBase<T> : ScriptableObject where T : *TypeToConstrainTo*
    3.     {
    4.        T MainSoField;
    5.        // ....
    6.     }
    7.  
    Next issue is that MySoBase is not a known type. MySoBase<T> is a known type... which is distinct from MySoBase. So you can't constrain to that.
     
    Krellumdaed likes this.
  4. Krellumdaed

    Krellumdaed

    Joined:
    Jun 24, 2015
    Posts:
    22
    Yes! That did it. I did not think of putting the base before the constraint. Perfect! Thanks!