Search Unity

Would the Roslyn Compiler compile C# 8.0 preview?

Discussion in 'Scripting' started by Igor_Vasiak, Dec 14, 2018.

  1. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    44
    I mean, I've been using Unity 2018.3 since the first beta for production, as it has always been for me. I really like the not-so-new already C# 7.0/1/2/3 features, but would the RC compile the preview version of the language (C# 8.0) that was released recently? It's just for the curiosity that I'm asking.

    Also, Unity says that the supported .NET Framework version is 4.x. Does that already include 4.8? Thanks.
     
  2. Deleted User

    Deleted User

    Guest

    What about trying and see if it works? :)
     
    Igor_Vasiak likes this.
  3. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    44
    I was hoping that someone had an answer already! xD
     
  4. Deleted User

    Deleted User

    Guest

    Well, it could be you to be the first to know and tell the others. :p
     
    Lurking-Ninja and Igor_Vasiak like this.
  5. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    44
    I just tested it. The C# 8.0 is only available on Visual Studio 2019 preview, which isn't compatible with Unity yet, apparently. I could be mistaken and have done something wrong, but I have no way of knowing if C# 8 does work with Unity because the VS version required doesn't in my hands. Sadface.

    Also, Unity doesn't support .NET 3.x Core preview. Sadface again. That's needed to compile C# 8.0, so I guess Unity can't compile C# 8.0 yet. I'm one of those guys, I want new features before they get invented/released.
     
    Last edited: Dec 14, 2018
  6. Deleted User

    Deleted User

    Guest

    I thought people had managed installing Visual Studio 2019 and have it work with Unity; maybe it was Unity 2019.1?
     
  7. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    44
    Maybe, I have no idea whatsoever. I'm not using an Alpha editor for production anyways. I'm testing it when it gets to Beta.
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    This is a 2018.3.0f2 project with integrated Roslyn v2.11.0.63519 (C# 8 beta) - https://dl.dropbox.com/s/insuh1830wfttk9/CSharp8.zip
    • Nullable reference types - ok
    • Ranges and indices - ok
    • Asynchronous streams - not yet, current mono runtime lacks some important core types and there's no easy way to add them externally
    Not for production, obviously. Just for fun.

    error.PNG
     
    Last edited: Dec 14, 2018
  9. Igor_Vasiak

    Igor_Vasiak

    Joined:
    Dec 13, 2016
    Posts:
    44
    If I could whistle via text, I would.

    EDIT: I know why I couldn't use VS19 with Unity. That's because sometimes I'm silly. I totally forgot to install the Unity Extension (or Tools or something like that) with VS. It's working perfectly now. Hurray!
     
    Last edited: Dec 15, 2018
  10. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    This is a 2018.4.3f1 project with integrated Roslyn 3.200.19.31205 taken from the latest Visual Studio 2019 version 16.2 Preview 3. You might also need to use that exact version of Visual Studio. I'm not sure it would work fine with older versions.

    https://dl.dropbox.com/s/izxjalqunrxlxei/CSharp8-2.zip

    Just for fun.
     
    Last edited: Jul 2, 2019
    SugoiDev likes this.
  11. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    @alexzzzz Using that means we lose the "incremental-ness" of the built-in one, right?

    I wish they had open sourced their changes so we could easily replace/modify anything to test out new features.
     
  12. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I doubt the stock compiler is incremental, is it?
     
  13. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Oh, I though it was? They deprecated the "incremental compiler package" stating that the interesting parts were ported over to the built-in one. It should also be somewhat parallel (independent asmdef stuff is compiled in parallel)

    Or maybe am crazy? I'll have to do some tests later.

    ---

    Alright, just tried to test but could not get it to work on 2019.3.0a8 (I did unblock the zip).
    Unity kept unloading the DLL because "this assembly can cause crashes in the runtime".
    I'll try to play with it a bit more when I have some free time.
     
    Last edited: Jul 26, 2019
  14. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Maybe I'm wrong, I remember it like "Here is our new compiler. It's not incremental, but works faster (or not slower) and almost as good as the incremental one which is now deprecated."

    I suspect that the stock compiler now is just Roslyn running on NET Core as a self-contained application. The Editor\Data\Tools\Roslyn folder looks like it is.
     
  15. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Yeah, maybe you're right.
    I remember trying to replace the Roslyn folder just to see if it would work, and it broke.
    But maybe that's because something else is compiled against a specific version of some library and that was what actually broke. The error messages were not helpful so I'm not sure.

    With the incremental compiler, I had a script that would fetch the latest Roslyn from GitHub and update it directly, so I was trying to modify my script to update the actual bundled Roslyn Unity was shipping with.

    Ah, I just remembered that I decompiled the csc and it was significantly different from Microsoft's, so maybe they are changing something.

    I'll try to play around with it more when I have some extra time.
     
  16. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,183
    Yes, starting with Unity 2018.3.
    https://forum.unity.com/threads/unity-incremental-c-compiler-deprecated.523993/page-10#post-3992170
     
    Igor_Vasiak, Peter77 and SugoiDev like this.
  17. SugoiDev

    SugoiDev

    Joined:
    Mar 27, 2013
    Posts:
    395
    Did anyone else play around with C#8 and Unity lately?
    I just saw that the "Implicit Nullability" resharper/rider plugin will reach end-of-life soon (sometime next year) because nullable types in C#8 supersede its features, then I remembered this thread.


    Here's their notice:

    End-of-life note: C# 8, supported by VS 2019+, will get nullable reference types which supersede - and perfectly align with - Implicit Nullability. Therefore ReSharper/Rider < 2020.1 will be the latest version supported by Implicit Nullability.

    And the reasoning, which is very fair. (Apologies for the screenshot, but I wanted the full github format)

    upload_2019-10-25_19-43-45.png


    I have a huge number of those NotNull and CanBeNull around.
    I also have more complex contractAnnotations, but those can't be easily replaced by nullable reference types.
     
    Peter77 likes this.
  18. rider-unity

    rider-unity

    Joined:
    Mar 13, 2019
    Posts:
    1
    @alexzzzz Wouldn't it make sense to name folder CSharp80Support instead of CSharp70Support? In Rider we could have used it to guess the LangVersion.
     
  19. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Putting the language version in the name wasn't the best idea. I realized that once you guys started using it to detect the plugin and the C# guys releasing new 7.x versions every 6 months. I decided it's safer to leave is as it is, like System32 folder in 64-bit Windows. I should have realized that earlier, when 7.0 came out and my repository's name was https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration.

    There is langVersion.txt file inside CSharp70Support folder. It is generated when the plugin's editor dll is loaded:
    Code (CSharp):
    1. [InitializeOnLoad]
    2. public static class CSharpVNextSupportActivator
    3. {
    4.     static CSharpVNextSupportActivator()
    5.     {
    6.         if (Directory.Exists(CustomCSharpCompiler.CSHARP_VNEXT_SUPPORT_DIR))
    7.         {
    8.             File.WriteAllText(Path.Combine(CustomCSharpCompiler.CSHARP_VNEXT_SUPPORT_DIR, "unityVersion.txt"), Application.unityVersion);
    9.             File.WriteAllText(Path.Combine(CustomCSharpCompiler.CSHARP_VNEXT_SUPPORT_DIR, "langVersion.txt"), "8.0");
    10.         }
    11. ...
    If it exists it should contain the most recent version of C# that the compiler is capable of, started with 7.2, then 7.3 and 8.0.

    The 8.0 version, posted in this thread, was mostly for fun. I'm not sure there's much sense in continuing the plugin further, since there's no longer a 10-year gap between Unity's C# and normal people's C#. Unity coders are almost normal people now officially.
     
    thedred112 and SugoiDev like this.