Last active
August 29, 2015 14:26
-
-
Save mzandvliet/575fcef3cb8ec6579297 to your computer and use it in GitHub Desktop.
Broken Value Type Wrapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is broken in Unity 5.x's built-in version of Mono, which is admittedly rather old. | |
// Compiling with VS2013 it works as intended. | |
public struct WrapperType<T> { | |
public float Timestamp; | |
public T Value; | |
} | |
public struct MyValueType { | |
public float floatA; | |
public float floatB; | |
public static void Test() { | |
var valInst = new MyValueType() { | |
floatA = 1234f, | |
floatB = 5678f | |
}; | |
var wrappedValInst = new WrapperType<MyValueType>() { | |
Timestamp = 0.135f, | |
}; | |
wrappedValInst.Value = valInst; | |
Debug.Log("try 1: " + wrappedValInst.Value.floatA + ", " + wrappedValInst.Value.floatB); | |
var identityWrappedValInst = Identity(wrappedValInst.Value); | |
Debug.Log("try 2: " + identityWrappedValInst.floatA + ", " + identityWrappedValInst.floatB); | |
// try 1: 1234, 5678 | |
// try 2: 0.135, 1234 | |
// Values have shifted to include WrapperType.Timestamp, what the... | |
// So where does the value of floatB end up? | |
} | |
public static MyValueType Identity(MyValueType a) { | |
return a; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment