背后的原因是什么

背后的原因是什么

本文介绍了添加“案例"背后的原因是什么?Scala 的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了:

case class A

... case 哪个很有用?

... case which is quite useful?

为什么我们需要在match中使用case?不会:

Why do we need to use case in match? Wouldn't:

x match {
  y if y > 0 => y * 2
  _ => -1
}

...要更漂亮和简洁?

... be much prettier and concise?

或者当一个函数接受一个元组时,为什么我们需要使用 case ?说,我们有:

Or why do we need to use case when a function takes a tuple? Say, we have:

val z = List((1, -1), (2, -2), (3, -3)).zipWithIndex

现在,不是:

z map { case ((a, b), i) => a + b + i }

... 方式比仅仅更丑:

z map (((a, b), i) => a + b + i)

...?

推荐答案

第二个问题,避免case的匿名函数,是一个有争议的问题:

The second issue, anonymous functions that avoid the case, is a matter of debate:

https://groups.google.com/d/msg/斯卡拉辩论/Q0CTZNOekWk/z1eg3dTkCXoJ

另外:http://www.scala-lang.org/old/node/1260

对于第一个问题,选择是允许箭头 RHS 上的块还是表达式.

For the first issue, the choice is whether you allow a block or an expression on the RHS of the arrow.

在实践中,我发现较短的 case body 通常更可取,因此我当然可以想象您的替代语法会产生更清晰的代码.

In practice, I find that shorter case bodies are usually preferable, so I can certainly imagine your alternative syntax resulting in crisper code.

考虑单行方法.你写:

def f(x: Int) = 2 * x

那么你需要添加一个语句.不知道IDE能不能自动添加括号.

then you need to add a statement. I don't know if the IDE is able to auto-add parens.

def f(x: Int) = { val res = 2*x ; res }

这似乎并不比要求 case body 的语法更糟糕.

That seems no worse than requiring the same syntax for case bodies.

回顾一下,case 子句是 case Pattern Guard =>正文.

To review, a case clause is case Pattern Guard => body.

目前,body 是一个块,或者一个语句序列和一个结果表达式.

Currently, body is a block, or a sequence of statements and a result expression.

如果 body 是一个表达式,你需要为多个语句使用大括号,比如函数.

If body were an expression, you'd need braces for multiple statements, like a function.

我不认为 => 会导致歧义,因为函数字面量不符合模式,不像 1"foo".

I don't think => results in ambiguities since function literals don't qualify as patterns, unlike literals like 1 or "foo".

一个障碍可能是:{ case foo =>???} 是一个模式匹配匿名函数"(SLS 8.5).显然,如果 case 是可选的或消除的,那么 { foo =>???} 是模棱两可的.您必须区分 anon funs 的 case 子句(其中需要 case)和 match 中的 case 子句.

One snag might be: { case foo => ??? } is a "pattern matching anonymous function" (SLS 8.5). Obviously, if the case is optional or eliminated, then { foo => ??? } is ambiguous. You'd have to distinguish case clauses for anon funs (where case is required) and case clauses in a match.

当前语法的一个反驳是,根据源自 C 的直觉,您总是暗地里希望您的匹配项能够编译为切换表.在那个比喻中,案例是要跳转到的标签,而标签只是一系列语句的地址.

One counter-argument for the current syntax is that, in an intuition deriving from C, you always secretly hope that your match will compile to a switch table. In that metaphor, the cases are labels to jump to, and a label is just the address of a sequence of statements.

替代语法可能会鼓励采用更内联的方法:

The alternative syntax might encourage a more inlined approach:

x match {
  C => c(x)
  D => d(x)
  _ => ???
}
@inline def c(x: X) = ???
//etc

这种形式看起来更像是一个调度表,匹配体回忆了Map语法,Map(a -> 1, b -> 2),即a关联的整洁简化.

In this form, it looks more like a dispatch table, and the match body recalls the Map syntax, Map(a -> 1, b -> 2), that is, a tidy simplification of the association.

这篇关于添加“案例"背后的原因是什么?Scala 的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 02:47