How can i make that

public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
where T : struct
{
stream.Serialize(ref key);

var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);

}
public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
where T : class
public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
where T : struct
{
stream.Serialize(ref key);

var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);

}
public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
where T : class
why is this not allowed? isnt the type restriciton enough? i want to differentiate between structs and classes so i have a null check where needed and no unnecessary boxing and stuff
7 Replies
Joreyk ( IXLLEGACYIXL )
how can i automatically say "value types go there and everything else goes there"
public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
{
stream.Serialize(ref key);
if(value == null)
{
stream.WriteNull();
}
else
{
var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);
}
}
public static void Write<T>(this IYamlStream stream, string key, ref T? value, DataStyle style = DataStyle.Any)
where T : struct
{
stream.Serialize(ref key);
if (value == null)
{
stream.WriteNull();
}
else
{
var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);
}
}
public static void Write<T>(this IYamlStream stream, string key, ref T value, DataStyle style = DataStyle.Any)
{
stream.Serialize(ref key);
if(value == null)
{
stream.WriteNull();
}
else
{
var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);
}
}
public static void Write<T>(this IYamlStream stream, string key, ref T? value, DataStyle style = DataStyle.Any)
where T : struct
{
stream.Serialize(ref key);
if (value == null)
{
stream.WriteNull();
}
else
{
var emitter = stream.Emitter;
stream.SerializeContext.Serialize(ref emitter, value, style);
}
}
having this and using
IYamlStream s2 = null;
var x = new Vector3(1f, 2f, 2f);
s2.Write("homp",ref x);
IYamlStream s2 = null;
var x = new Vector3(1f, 2f, 2f);
s2.Write("homp",ref x);
redirects me to the first implementation.. but how can the first implementation check the vector for null?
canton7
canton79mo ago
Why can't you just have one unconstrained implementation?
Joreyk ( IXLLEGACYIXL )
what does the unconstrained implementation with structs?
canton7
canton79mo ago
I don't understand the question If you check whether T is null, that's just a no-op if T is a value type
Joreyk ( IXLLEGACYIXL )
what is a no op?
canton7
canton79mo ago
Iirc the JIT might even remove that code branch entirely when it emits implementations if that method for struct A no-op is "no operation" - something which doesn't do anything The jit has to compile a separate copy of your method for when T is a reference and a value type. When it compiles the value type version, it'll skip all code which doesn't make sense for value types, like null checks So there's no harm in checking whether a value type is null.
Joreyk ( IXLLEGACYIXL )
ah goody oke then i will go with the second implementation as nullables are a problem type for me thanks
Want results from more Discord servers?
Add your server