Replies: 10 comments
-
Any change to the value is not detectable. For example (in release mode), this: public void M() {
Vector3 v = GetValue();
if(v is Vector3 v2)
{
v2.Z = v2.Y;
v2.Y = 1;
}
} compiles as: public void M()
{
Vector3 value = GetValue();
value.Z = value.Y;
value.Y = 1f;
} whereas this: public void M() {
Vector3 v = GetValue();
if(v is Vector3 v2)
{
v2.Z = v2.Y;
v2.Y = 1;
}
Console.WriteLine(v.Y);
} compiles as: public unsafe void M()
{
Vector3 value;
Vector3 expr_06 = value = GetValue();
value.Z = value.Y;
value.Y = 1f;
Console.WriteLine(expr_06.Y);
} |
Beta Was this translation helpful? Give feedback.
-
No. A pattern variable is a fresh variable, not a "ref-like" reference to some other variable. |
Beta Was this translation helpful? Give feedback.
-
@gafter Thanks. Is there a reason for it or it just not possible? |
Beta Was this translation helpful? Give feedback.
-
Pattern-matching is part of a longer-term effort to make it easier to program with immutable data, without mutation. Making pattern variables be refs is inconsistent with that design goal. Besides, there usually is not a “container” for the thing you are matching against, which could be the referent of the ref. |
Beta Was this translation helpful? Give feedback.
-
@gafter Thanks for your clarification. Sadly I wish we could have generic function for modifying value Such as void ResetPosition<T>(ref T value) where T : struct
{
// DoSomeThing
switch(value)
{
case Vector3 v:
v.X = 0;
v.Y = 0;
v.Z = 0;
break;
case Matrix4x4 m:
m.M41 = 0;
m.M42 = 0;
m.M43 = 0;
break;
}
// DoSomeOtherThing
} |
Beta Was this translation helpful? Give feedback.
-
I may be missing something, but you appear to be "hijacking" both generics and pattern matching to achieve something that can already be done more simply using a delegate: delegate void RefAction<T>(ref T value);
void ResetPosition<T>(ref T value, RefAction<T> typeSpecificReset) where T : struct
{
// DoSomeThing
typeSpecificReset(ref value);
// DoSomeOtherThing
}
void ResetMatrix4x4(ref Matrix4x4 matrix)
{
void ResetCoords(ref Matrix4x4 m) => (m.M41, m.M42, m.M43) = (0, 0, 0);
ResetPosition(ref matrix, ResetCoords);
} Wanting to use pattern matching to enable casting on ref structs seems a pretty nasty abuse of the feature to me. |
Beta Was this translation helpful? Give feedback.
-
@DavidArno I know but using delegate defeat the purpose of efficiency using ref struct |
Beta Was this translation helpful? Give feedback.
-
Why do you need a generic function? It's obvious that the example can be converted to use function overloading. Unless that function is supposed to be called from other generic code. Is that the case? |
Beta Was this translation helpful? Give feedback.
-
Have you tested this, or are you just assuming? |
Beta Was this translation helpful? Give feedback.
-
@DavidArno At the very least delegate itself is created to be object on heap. Frequency of creation varied by implementation in runtime @mikedn It more clean to have same before logic and after logic in the same function. Overloading make many duplicate code in this case |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Suppose
Would the code in
if
block change the value ofv
and should it be?I was also thinking for the case when we could map the ref generic struct
I don't know if it possible but I mean
Beta Was this translation helpful? Give feedback.
All reactions