Search Unity

T? is not blittable

Discussion in 'Entity Component System' started by sient, Jun 13, 2018.

  1. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    It seems that T? is not blittable, ie, the following will throw a blittable validation exception.

    Code (CSharp):
    1. struct MyData : IComponentData {
    2.   public int? value; // not blittable
    3. }
    I've written simple replacement, but it'd be great to not require this.

    Code (CSharp):
    1. struct MyData : IComponentData {
    2.   public Opt<int> value; // blittable
    3. }
    4.  
    5. /// <summary>
    6. /// T? simulation that can be used in IComponentData
    7. /// </summary>
    8. public struct Opt<T> {
    9.     T value_;
    10.  
    11.     public Bool HasValue;
    12.     public T Value {
    13.         get {
    14.             if (!HasValue)
    15.                 throw new InvalidOperationException("!HasValue");
    16.             return value_;
    17.         }
    18.     }
    19.  
    20.     public static implicit operator bool(Opt<T> opt) {
    21.         return opt.HasValue;
    22.     }
    23.     public static implicit operator Opt<T>(T value) {
    24.         return new Opt<T> {
    25.             value_ = value,
    26.             HasValue = true,
    27.         };
    28.     }
    29. }
     
  2. Afonso-Lage

    Afonso-Lage

    Joined:
    Jul 8, 2012
    Posts:
    70
    int?
    is a shorthand for
    Nullable<int>
    and, as you can see on reference source, Nullable<T> has a boolean and a T value, and as you can see here also, boolean is not a blittable type, so in order to a struct be blittable, all its members must be blittable.

    Why boolean isn't blittable? Because the way it is stored internally on memory vary on managed and non-managed code and blittable types must be stored on the same way, be it managed or non-managed.
     
    forestrf likes this.
  3. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    nullable types (T? / Nullable<T>) are actually value types, consisting in the value + a 'has value' flag.
    then the compiler does it's magic so "x == null", assignment and operators work.

    the problem is that Nullable<T> contains a bool, and bool is not blittable.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Yes, my mistake, sleepy wrote nonsense :) Yes of course Nullable is a value type (structure as in any way)
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
  6. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Unity doesn't allow some things even when they are blittable it seems. Such as arrays of blittable types which are supposed to be blittable, or even their own Collections.
     
  7. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    328
  8. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116
    Well, arrays are blittable. I guess the second part is the problem, making any struct containing them unblittable.
     
  9. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    Ah, right, when Unity fixes the bool IsBlittable check hopefully this will work.

    Unity's definition of blittable is different from the .NET definition. For Unity, anything that holds managed memory is not blittable.
     
  10. Necromantic

    Necromantic

    Joined:
    Feb 11, 2013
    Posts:
    116