Inferred delegate types have been introduced since C# 10.
推断委托类型是从C#10开始引入的。
However, I am quite confused with the behavior of the feature:
然而,我对该功能的行为感到相当困惑:
Generally, I think we can reasonably expect that, if this can compile:
一般来说,我认为我们可以合理地预期,如果这一点能够实现的话:
var x = (my_expression); // inferred type
SomeType y = x;
This single assignment should compile as well:
这个单一的赋值也应该编译:
SomeType y = (my_expression);
However, this is not satisfied when inferred delegate types are involved:
但是,当涉及推断的委托类型时,这是不能满足的:
using System;
class Program {
class FunctionWrapper<Output> {
private Func<Output> WrappedFunction { get; }
public FunctionWrapper(Func<Output> function) => WrappedFunction = function;
public static implicit operator FunctionWrapper<Output>(Func<Output> function) => new FunctionWrapper<Output>(function);
}
public delegate void DoStuffDelegate<Inout>(ref Inout Input);
static void Main(string[] args) {
var funcLambda = () => "hello"; // type is inferred here...
FunctionWrapper<string> funcWrapper = funcLambda; // ... it compiles
// but if we do a single assignment...
FunctionWrapper<string> funcWrapper2 = () => "hello"; // CS1660 - this does not compile anymore... Why?
// the exact opposite happens with delegates:
// if we do a single assignment...
DoStuffDelegate<string> delegWrapper2 = (ref string input) => { input = input + input; }; // ... it compiles
// but if type is inferred...
var delegateLambda = (ref string input) => { input = input + input; }; // type is inferred
DoStuffDelegate<string> delegWrapper1 = delegateLambda; // CS0029 - this does not compile anymore...
}
}
I have the impression that inferred delegate types imply an implicit conversion at some stage, which probably should not be the case.
我的印象是,推断的委托类型在某个阶段暗示了隐式转换,但情况可能并非如此。
(link to the code: https://godbolt.org/z/3sbd9ndrT)
(代码链接:https://godbolt.org/z/3sbd9ndrT)
I tried different compilers and searched for documentation, discussions, and potential known issues regarding the behavior of inferred delegate types, unsuccessfully.
我尝试了不同的编译器,并搜索了有关推断的委托类型的行为的文档、讨论和潜在的已知问题,但都没有成功。
Can somebody please tell if this is a language/compiler defect, or if my expectations for these conversions to work are unreasonable?
有人能告诉我这是语言/编译器缺陷,还是我对这些转换的期望是不合理的?
Thank you in advance.
提前谢谢您。
更多回答
Looks like an issue with IDE0004
.
看起来像是IDE0004的问题。
You should report this issue
您应该报告此问题
更多回答
I agree that IDE0004 is wrong in the sense that it is not aligned with compiler behavior. However, I am still questionning the language and the compiler behavior here. If "var x = expr; SomeType y = x;" compiles, doing directly "SomeType y = expr;" should compile as well, right?
我同意IDE0004是错误的,因为它与编译器行为不一致。然而,我仍然在质疑这里的语言和编译器行为。如果“var x=expr;SomeType y=x;”编译,则直接执行“SomeType y=expr;”也应该编译,对吗?
IDE0004 is not a compiler error. You can try sharplab.io where you won't get IDEXXXX diagnostics or a command-line compilation.
IDE0004不是编译器错误。您可以在不会获得IDEXXXX诊断或命令行编译的地方尝试Sharplab.io。
Yes, I know IDE0004 is not a compiler error. IDE0004 is not the main subject of the initial question, sorry if I was not clear. You can ignore mentions of IDE0004. The question is about the the compilation errors indeed.
是的,我知道IDE0004不是编译器错误。IDE0004不是最初问题的主要主题,如果我说得不清楚,请原谅。您可以忽略对IDE0004的提及。这个问题是关于编译错误的。
What compilation errors are you getting?
您收到了哪些编译错误?
All the ones included in the code snippet I provided - If you enable the code that is commented out, you should get them as well.
我提供的代码片段中包含的所有代码--如果启用被注释掉的代码,也应该得到它们。
我是一名优秀的程序员,十分优秀!