[Proposal]: Allow struct to return instance members by reference (avoid CS8170) #6565
Replies: 12 comments 1 reply
-
This already of a part of #3936. |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan That's why I propose an alternative that we should keep the same behaviour of In the presense of difference keyword, the compiler would be able to distinguish that it need to treat any value returned from that function in the same way as reference to the field value directly. And deal with it in the same lifetime of And as I present in the issue, with this we could declare interface in the same way, so we could write generic method with this constraint, for arbitrary operation and access with struct |
Beta Was this translation helpful? Give feedback.
-
There's no difference with adding a new attribute. Abusing keyword is strictly avoided. |
Beta Was this translation helpful? Give feedback.
-
@huoyaoyuan If the attribute could enforce compiler if specified on interface and also force the struct to implement this function to also declare the same attribute then I would be satisfied, will it though? |
Beta Was this translation helpful? Give feedback.
-
Why would it be useful for an interface to be able to constrain the method in this way? |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt The major use case of this feature I would like to have is as the sample above. The ability to declare an arbitrary size vector and matrix struct, then I could write one generic extension method for every of them I could made a struct that could act like 34 dimension float vector. And a struct that act like 34x11 dimension float matrix And then I could write a dot product as extension method that constraint with In my opinion this is the major use case of generic constraint in general. We can have interface as contract for struct to implement, then we can write any generic function to act on any struct with the same contract and same pattern efficiently. The current hindrance to efficiency of this method is that, we can't have struct return ref of its field from the generic constraint function |
Beta Was this translation helpful? Give feedback.
-
Could you link to a gist showing what you would like to write here? |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt Or did you mean, why interface should also declare that its function is return the Then it was also because this exact use case, generic constraint with public static ref float GetRef<T>() where T : IVector
{
var v = new T();
return ref v[0]; // is this fine?
} |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi Do you mean my I would like to reuse all of my public static class VectorExt
{
public static float Dot<V>(in this V l,in V r) where V : struct,IVector
{
float sum = 0;
int i = 0;
while(i < l.Size)
{
sum += l[i] * r[i];
i++;
}
return sum;
}
public static float Dot<V0,V1>(in this V0 l,in V1 r)
where V0 : struct,IVector
where V1 : struct,IVector
{
if(l.Size != r.Size)
throw new ArgumentOutOfRangeException("vector Size");
float sum = 0;
int i = 0;
while(i < l.Size)
{
sum += l[i] * r[i];
i++;
}
return sum;
}
public static void Dot<V,M,R>(in this V vector,in M matrix,out R result)
where V : struct,IVector
where M : struct,IMatrix
where R : struct,IVector
{
if(vector.Size != matrix.Width)
throw new ArgumentOutOfRangeException("vector Width");
if(result.Size != matrix.Height)
throw new ArgumentOutOfRangeException("result Height");
result = new R();
int i = 0;
while(i < r.Size)
{
result[i] = vector.Dot(matrix[i]); // vector dot vector using above extension method
i++;
}
}
}
////
struct MyInputVector : IVector
{
// 34 float
}
struct MyMatrix : IMatrix
{
// 34x11 float
}
struct MyOutputVector : IVector
{
// 11 float
}
////
var input = new MyInputVector();
var matrix = new MyMatrix();
input.Dot(matrix,out MyOutputVector output); // Dot<V,M,R> => Dot<MyInputVector,MyMatrix,MyOutputVector> something like this |
Beta Was this translation helpful? Give feedback.
-
Anything new about this topic, since it was not closed since then? |
Beta Was this translation helpful? Give feedback.
-
There is nothing new on this topic. As per our README, I'm converting this to a discussion topic. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Introduce new syntax to have
ref return
function and property and indexer possible to return field of structMotivation
Main motivation is to allow access to reference of struct member by function and indexer
Take
Matrix
for example. It would be convenient to access matrix with index like soDetailed design
As the example above. I think it should be possible to use some keyword,
in
in this case, as a replacement ofref
. It actually just normalref return
but with a difference marker to let the stack know that, the returned reference will be a reference of that struct instance field itself, so it still return errorCS8170
if trying to return it over the stack that instantiate itFor example, with the same
Matrix4x4
aboveThis would also allow interface member to be defined as the reference of the field
Drawbacks
I don't think there is any?
Alternatives
Extension method is the closest possible solution but it not as flexible as it not allow implementing for interface, maybe unless something like
trait
also exist too?Unresolved questions
If the
in
keyword would be confused then what could be?Design meetings
Also related to #2107
Beta Was this translation helpful? Give feedback.
All reactions