Is it possible to somehow compile XM() { /*...*/ }
given the following constraints:
M
. /*...*/
does not contain return
or throw
, and you can't change it. M
. It might seem like a nonsensical question, but it is actually valuable for DSLs.
Eg I could define something like task M() {}
which would just denote that it is a task, and have nothing to do with a return value.
Adding in some additional information from the C#
specification.
This first paragraph states that a method must have a return type, or void.
Methods have a (possibly empty) list of parameters, which represent values or variable references passed to the method, and a return type, which specifies the type of the value computed and returned by the method. A method's return type is void if it does not return a value.
This second paragraph specifies that a method with a non-void return type must have a calculable return expression.
A method can use return statements to return control to its caller. In a method returning void, return statements cannot specify an expression. In a method returning non-void, return statements must include an expression that computes the return value.
Even though the specification does not explicitly state that method must contain a return statement, I believe it implicitly says so.
So no, it is not possible according to the C#
specification.
So you have a method:
public Foo Bar()
{
DoX();
DoY();
DoZ();
}
Which doesn't compile, because the only path in the method doesn't return anything.
Given your constraints (basically: you have to compile the code as-is, with a standard C# compiler), the answer is: no, you can't make this compile.
maybe,
abstract class Whatever
{
public abstract X M<X>();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.