模式匹配增强:Switch 模式

huangapple go评论60阅读模式
英文:

Pattern Matching Enhancement: Switch Pattern

问题

以下是您要翻译的内容:

刚刚看了一下"C# 8.0 Features" 1

所以我尝试重写以下代码

private static void RunExample(ExampleCode exampleCode)
{
    switch(exampleCode)
    {
        case ExampleCode.DefaultInterfaceMethod:
            RunDefaultInterfaceMethodExample();
            break;
        case ExampleCode.PatternMatchingEnhancements:
            RunPatternMatchingEnhancementsExample();
            break;
    }      
}

为了这个:

private static void RunExample(ExampleCode exampleCode)
{
    exampleCode switch
    {
        ExampleCode.DefaultInterfaceMethod => RunDefaultInterfaceMethodExample(),
        ExampleCode.PatternMatchingEnhancements => RunPatternMatchingEnhancementsExample()
    };           
}

然而,我得到了以下编译错误:

只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句。

模式匹配增强:Switch 模式

我该如何在新语法中重写这个?

英文:

Just had a look at the "new" C# 8.0 Features

So I tried to rewrite the following code

    private static void RunExample(ExampleCode exampleCode)
    {
        switch(exampleCode)
        {
            case ExampleCode.DefaultInterfaceMethod:
                RunDefaultInterfaceMethodExample();
                break;
            case ExampleCode.PatternMatchingEnhancements:
                RunPatternMatchingEnhancementsExample();
                break;
        }      
    }

to this:

    private static void RunExample(ExampleCode exampleCode)
    {
        exampleCode switch
        {
            ExampleCode.DefaultInterfaceMethod => RunDefaultInterfaceMethodExample(),
            ExampleCode.PatternMatchingEnhancements => RunPatternMatchingEnhancementsExample()
        };           
    }

However, I am getting the following compile error:
> Only assignment, call, increment, decrement, await, and new object
> expressions can be used as a statement.

模式匹配增强:Switch 模式

How can I rewrite this in the new syntax?

答案1

得分: 3

以下是翻译好的部分:

根据评论所指出的,新的基于开关的模式匹配期望返回结果。

F# 中,因为一切都是表达式,void 类型实际上是表达式的有效返回类型,这将起作用。

在像你这样的情况下,我认为最好使用旧代码,但如果你真的想使用新的语法,你可以像这样做:

Action methodToExecute = exampleCode switch
{
  ExampleCode.DefaultInterfaceMethod => RunDefaultInterfaceMethodExample,
  ExampleCode.PatternMatchingEnhancements => RunPatternMatchingEnhancementsExample,
  _ => throw new NotImplementedException()
};
methodToExecute();

(仅当你为每个情况执行的方法具有相同的定义时才有效)

使用详尽的模式是一个好的做法,这就是为什么我在最后一个情况中使用了下划线。

在 C# 中,枚举值编译为整数,即使你的开关处理了所有枚举标签,编译器仍然不知道你已经处理了所有情况,当你向枚举添加新标签时,你将没有适当的警告表明你有未处理的情况。

无论何时使用枚举,最好使用默认情况,所有未处理的情况都会落入其中。

英文:

As pointed in the comments the new switch based pattern matching is expecting result to be returned.

In F# because everything is an expression, the void type is actually a valid return type for an expression and this would have worked.

In case like yours i think it is best to use the old code, but if you really want to use the new syntax you can do something like this:

Action methodToExecute = exampleCode switch
{
  ExampleCode.DefaultInterfaceMethod => RunDefaultInterfaceMethodExample,
  ExampleCode.PatternMatchingEnhancements => RunPatternMatchingEnhancementsExample,
  _ => throw new NotImplementedException()
};     
methodToExecute();

(this will work only if the methods you are executing for each case have the same definitions)

It is good practice to use exhaustive pattern, this is why i am using the last case with the underscore.
In C# enum values are compiled to integers and even if your switch handles all enum labels the compiler still does not know you have handled all cases and when you add new label to the enum you won't have proper warrning that you are have unhandled case.

Whenever you are using enums it is best to use default case where all unhandled cases will fall in.

huangapple
  • 本文由 发表于 2020年1月6日 22:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定