Replies: 13 comments
-
How would overload resolution work in that case? Would the |
Beta Was this translation helpful? Give feedback.
-
If you use |
Beta Was this translation helpful? Give feedback.
-
Yes, properties please:
Instead of:
|
Beta Was this translation helpful? Give feedback.
-
@janjoostvanzon That already works, this si valid C# 7 code (assuming public ref string Text => ref labelTitle.Text; The question here is whether it should be allowed to have both versions at the same time: public ref string Text => ref labelTitle.Text;
public string Text
{
get => labelTitle.Text;
set => labelTitle.Text = value;
} |
Beta Was this translation helpful? Give feedback.
-
labelTitle.Text is not a field. It's a property. |
Beta Was this translation helpful? Give feedback.
-
@janjoostvanzon Then you can't return it by |
Beta Was this translation helpful? Give feedback.
-
I understand now, that your question is about resolving the ambiguity between similar members that only differ by whether they are ref or not. I must have been too sleepy when I responded. However, you gave me ideas, not directly related to your question. |
Beta Was this translation helpful? Give feedback.
-
Since there can also not be overloads that differ by just the return type, it seems strange that there could be overloads that only differ by whether they return ref. Does that make sense? I question whether you should be able to add both a ref and non-ref variation at all, and whether you should be able to see from the outside whether it is ref at all: So I think this:
could be a better syntax than:
If the language worked like that, your compatibility / ambiguity issue would be gone. |
Beta Was this translation helpful? Give feedback.
-
@janjoostvanzon Even if the language did work like that the CLR does not. The return type is either What concerns me about this proposal is how it would play out when you attempt to consume that method from other languages. Does the CLS allow for overloading based only on return type? What is the stance on |
Beta Was this translation helpful? Give feedback.
-
Now that I think about it, I wonder if supporting named indexers (#471) and using them wouldn't be a cleaner solution. E.g.: class ArrayBuilder<T>
{
// might need a better name
public ref T ByRef[int index]
=> ref array[index];
public T this[int index] {
get => array[index];
set => array[index] = value;
}
}
…
ref var item = ref builder.ByRef[index]; // calls the ref indexer
var item = builder[index]; // calls the non-ref indexer It means using the by-ref indexer is more verbose, but since it's a fairly niche feature, that might be okay. And it also means avoiding confusing overloading by return type. |
Beta Was this translation helpful? Give feedback.
-
In that case, we could be using a regular method instead; I don't think using a pair of brackets instead of parentheses is worth doing a feature (#471), also, you are suggesting an alternative that requires slightly more work (like syntax change and dealing with ambiguities) and IMO it would not get past 1000 points to make the cut. ref/non-ref indexers already supported for arrays and it wouldn't be ambigious in any way. I think clr support is a good opportunity to relax rules and take advantage of the mandatory |
Beta Was this translation helpful? Give feedback.
-
@alrz |
Beta Was this translation helpful? Give feedback.
-
I don't believe that's the case for a ref-returning indexer. |
Beta Was this translation helpful? Give feedback.
-
It helps you to provide ref indexers without breaking existing code.
Usage:
Note: afaik clr is capable of overloading based on return type.
Open question: should we restrict this so that both indexers required to return the same type?
Open question: is it useful to allow this also for other members?
Beta Was this translation helpful? Give feedback.
All reactions