Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Broken Covariance Support in Unity's Version of C#

Discussion in 'Scripting' started by Zergling103, Apr 20, 2016.

  1. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm surprised it works at all. Most of the exotic new language features don't work.

    If anything the bug is that it didn't throw you a compiler error saying "unexpected symbol out".
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    When are Unity updating from .NET 2.0? That one's over a decade now
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Soon. The current plan is to roll out IL2CPP across all platforms. Once this is done upgrading the mono/.NET version becomes much easier.

    There was a bunch of lisencing issues around xarmin and getting the latest mono. However those have changed recently with the Microsoft acquisition. However UT has indicated they still think completing IL2CPP is the best thing for the engine.
     
    Nigey likes this.
  6. Zergling103

    Zergling103

    Joined:
    Aug 16, 2011
    Posts:
    392
    It's strange because assignment works, or it least it seems to. Meaning you can cast an IBasket<Apple> to an IBasket<Fruit> and it works. It's just it seems you can't use is or as to determine if that will throw an exception or not. :(
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    So after looking at your stackoverflow post...

    Covariance and especially contravariance isn't supposed to be in the outdated version of C# we get in Unity. And you seem to understand that part of it.

    But what's weird is the behaviour of how the code compiles for certain aspects of it, giving what appears to be a certain level of covariance, but contradictory results (a variable typed as A but returns false if tested if it 'is' an A).

    I'm willing to bet that the version of the compiler Unity is using is probably based on a version from the mono project when they were first implementing newer features like covariance and contravariance (new for the time that is), but not yet finished. Which allows the compiler to accept it in some conditions, but the runtime to not act correctly despite it.
     
    Last edited: Apr 20, 2016
    Kiwasi likes this.