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

Cannot implicitly convert type `System.Collections.Generic.List<T>'

Discussion in 'Scripting' started by IanNicolades, Nov 24, 2018.

  1. IanNicolades

    IanNicolades

    Joined:
    Oct 1, 2016
    Posts:
    40
    I have a bunch of code that builds and runs perfectly fine in Visual Studio, targeting .NET 4.5. It utilizes a generic list pool like this:

    Code (CSharp):
    1. List<BytecodeInstruction> bodyInstructions = ListPool<BytecodeInstruction>.Allocate();
    2.  
    3. internal static class ListPool<T>
    4. {
    5.     public static List<T> Allocate()
    6.     {
    7.         return SharedPools.Default<List<T>>().AllocateAndClear();
    8.     }
    9.     ...
    10. }
    Unity is throwing an error on every occurrence of this, however:

    Code (CSharp):
    1. error CS0029: Cannot implicitly convert type `System.Collections.Generic.List<T>' to `System.Collections.Generic.List<ScriptEngine.BytecodeInstruction>'
    I'm not sure why Unity is having trouble compiling it when VS is not. I've set Unity's scripting runtime version to .NET 4.x Equivelent, API compatibility level to 4.x, and Mono as the scripting backend. I would ideally like to not have to rewrite this pattern, as not using generics would significantly complicate an already very complex, mature codebase.
     
  2. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    What happens when you explicitly upcast it?
     
  3. IanNicolades

    IanNicolades

    Joined:
    Oct 1, 2016
    Posts:
    40
    Code (CSharp):
    1. List<BytecodeInstruction> bodyInstructions = ListPool<BytecodeInstruction>.Allocate() as List<BytecodeInstruction>;
    Seems to compile, but I'm concerned because the cast should not need to be explicit.

    Especially because of the opposite case, where that isn't possible:

    Code (CSharp):
    1. ListPool<BytecodeInstruction>.ReturnAndFree(bodyInstructions);
    2.  
    3. ...
    4.  
    5. public static List<T> ReturnAndFree(List<T> list)
    6. {
    7.      SharedPools.Default<List<T>>().ForgetTrackedObject(list);
    8.                 return list;
    9. }
    I just need to know what the difference between the specs are - and if there's some way to get it to compile the same way as it does under .NET 4.5.

    The other curious thing about this is that I have VS configured as Unity's code editor, and yet it never shows any of these errors while editing them - only in the Unity console.
     
  4. ZombieTFK

    ZombieTFK

    Joined:
    Sep 6, 2016
    Posts:
    55
    Hmm, I'd imagine there must be some sort of disconnect between what VS is using for it's static analysis and what unity is actually using to compile then, either between the .net core/mono runtimes or the actual version of the language being used. Looking briefly at the c# feature list between versions of the language and the .net versions it doesn't look like there's anything which would cause a breaking change like this... I'm no c# expert though. The build options in Unity aren't exactly the most explicit for this sort of stuff, sadly.
     
  5. IanNicolades

    IanNicolades

    Joined:
    Oct 1, 2016
    Posts:
    40
    Definately agree, much is left to the imagination! At the suggestion of some kind folks on the Game Dev League discord, updating the Unity project to 2018.3.0b11 seems to have resolved the issue. It disappeared just as mysteriously as it arrived! :D
     
    ZombieTFK likes this.