diary

日記です

Unity 5.5以前ではGenericsクラスのコンストラクタのデフォルト値としてconstを参照してはいけない

TL;DR

  • Unity 5.5以前(monoの更新前)ではGenericsクラスのコンストラクタのデフォルト値としてconstを参照してはいけない
    • メンバメソッドの名前解決ができなくなる
    • なぜかフィールドは名前解決ができる
  • Unity 5.5以降にすることですべては解決する

詳しく

以下は通らない

    public class ConstInConstructor<T>
    {
        private const int Const = 1;
        private int field = 0;
        private event Action SomeEvent;
        private Action SomeDelegate;
        public ConstInConstructor(int arg = Const)
        {
        }

        private void InnerLogic()
        {
        }
        
        public void Execute()
        {
            field = 1; // ok
            SomeDelegate(); // ok
            SomeEvent(); // error CS0103: The name `SomeEvent' does not exist in the current context
            InnerLogic(); // error CS0103: The name `InnerLogic' does not exist in the current context
        }
    }

以下は通る

    public class NotConstInConstructor<T>
    {
        private int field = 0;
        private event Action SomeEvent;
        private Action SomeDelegate;
        public NotConstInConstructor(int arg = 1)
        {
        }

        private void InnerLogic()
        {
        }
        
        public void Execute()
        {
            field = 1; // ok
            SomeDelegate(); // ok
            SomeEvent(); // ok
            InnerLogic(); //ok
        }
    }