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. The 2023.1 beta is now available for testing. To find out what's new, have a look at our 2023.1 feature highlights.
    Dismiss Notice

Question Will Unity ever support nullable reference type in its library?

Discussion in '2023.1 Beta' started by Elmroot, Nov 6, 2022.

  1. Elmroot

    Elmroot

    Joined:
    Jan 3, 2022
    Posts:
    2
    I mean, e.g. GetComponent<T>() should return not just T, but T?, and other similar things.
    Is there even a plan to do that in the future?
     
    kdserra and NoTuxNoBux like this.
  2. brunocoimbra

    brunocoimbra

    Joined:
    Sep 2, 2015
    Posts:
    647
    There is no difference when compared to the current API. All reference types have the possibility to be null by default, it would only make for things like TryGetComponent where if true it could have a hint for the IDE that output will not be null.
     
  3. Holygoe

    Holygoe

    Joined:
    Mar 3, 2015
    Posts:
    23
    The point is that, there will be no warning for the example below, but it should be if nullables is enabled
    Code (CSharp):
    1. public class Card : MonoBehaviour
    2. {
    3.     [SerializeField] private Card _parent = null!;
    4.  
    5.     private void Awake()
    6.     {
    7.         _parent = transform.parent.GetComponent<Card>();
    8.     }
    9. }
     
    Last edited: Nov 6, 2022
    kdserra likes this.
  4. atcarter714

    atcarter714

    Joined:
    Jul 25, 2021
    Posts:
    38
    Does this not do what you want? I use the compiler flag #nullable enable and I'm able to use nullables in whatever code I want, pretty much. Your original example code also shows the other way to get rid of the warnings by using the ! symbol at the end of a name/identifier, which is essentially telling the compiler "I know what I'm doing here, it'll be fine" and it just drops the issue and the warning is gone. I feel a little guilty for doing that sometimes and telling sweet Rosalyn to shut up, lol, but it's honestly a pretty valid way to get rid of an unhelpful warning message you don't need and when you're fully aware of what's going on and why she's nervous.

    Just try the flag I've mentioned above and demonstrated below and see if that isn't exactly what you want/needed though ...

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. #nullable enable
    5.  
    6. public class Card : MonoBehaviour
    7. {
    8.     [SerializeField] private Card? _parent = null;
    9.  
    10.     private void Awake() {
    11.         _parent = transform.parent.GetComponent<Card>();
    12.     }
    13. }
     
  5. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    692
    However, Unity C# is not compiled with nullable enabled, so all reference types are automatically nullable. Thus, with your own nullable enabled code, there are also null waring of methods that actually never return null. In addition, Unity also needs its own specific nullable settings that take into account Awake and Start. (And RequiredComponent attribute)
     
  6. Holygoe

    Holygoe

    Joined:
    Mar 3, 2015
    Posts:
    23
    Of course not :)
    private Card _parent = null! – It is just a way to suspend the erroneous warning for non-nullable field with the lack of constructor. And I hope that trick will be unnecessary in the future.
     
  7. NoTuxNoBux

    NoTuxNoBux

    Joined:
    Oct 2, 2020
    Posts:
    26
    The situation with Unity right now is indeed not ideal, even if you enable it yourself for your own code.

    Whilst semantically true right now, the plan for .NET is to move towards nullable reference types by default (in fact .NET 6 seems to have already enabled it by default), so Unity should follow suit at some point if they intend to keep up with .NET (which they seem to be working on), and having this in place sooner rather than later is beneficial as it allows gradual migration.
     
  8. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    692
    I just recently heard in a video on the dotnet channel on youtube, in .NET 7 the entire Framework now is compiled nullable enabled warning free. However, that only went with the help of the community due to the size of the framework.
     
    NoTuxNoBux likes this.