Scala 中语法糖的所有实例是什么?
Scala 中语法糖的所有实例是什么?
它们很难搜索,因为大多数/全部都是纯粹的符号,因此在不知道概念名称的情况下很难搜索。
TODO:
- 隐式转换
- 匿名函数的
_
语法我忘记的其他事情
What are all the instances of syntactic sugar in Scala?
They are hard to search for since most/all of them are purely symbols and are thus hard to search for without knowing the name of the concept.
TODO:
- Implicit conversions
_
syntax for anonymous functions- Other things I'm forgetting
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
基础知识:
a b
相当于ab
。ab c
等同于ab(c)
,除非b
以:
结尾。在这种情况下,ab c
相当于cb(a)
。a(b)
相当于a.apply(b)
这就是为什么以下匿名函数的定义是相同的:当调用
square1(y)
时,您实际上是在调用square1.apply(y)
,其中square1
必须具有< code>Function1 特征(或Function2
等...)a(b) = c
等价于a.update(b,c)
。同样,a(b,c) = d
相当于a.update(b,c,d)
等等。ab = c
相当于a.b_=(c)
。当您在类/对象中创建val
/var
x
时,Scala 会创建方法x
和>x_=
为您服务。您可以自己定义这些,但如果您定义y_=
,则必须定义y
,否则它将无法编译,例如:< code>-a 对应于
a.unary_-
。对于+a
、~a
和!a
也是如此。a= b
,其中
是一组特殊字符,相当于a = a; b
仅如果a
没有=
方法,例如:Basics:
a b
is equivalent toa.b
.a b c
is equivalent toa.b(c)
, except whenb
ends in:
. In that case,a b c
is equivalent toc.b(a)
.a(b)
is equivalent toa.apply(b)
This is why the following definitions for an anonymous functions are identical:When calling
square1(y)
, you are actually callingsquare1.apply(y)
whichsquare1
must have as specified by theFunction1
trait (orFunction2
, etc...)a(b) = c
is equivalent toa.update(b,c)
. Likewise,a(b,c) = d
is equivalent toa.update(b,c,d)
and so on.a.b = c
is equivalent toa.b_=(c)
. When you create aval
/var
x
in a Class/Object, Scala creates the methodsx
andx_=
for you. You can define these yourself, but if you definey_=
you must definey
or it will not compile, for example:-a
corresponds toa.unary_-
. Likewise for+a
,~a
, and!a
.a <operator>= b
, where<operator>
is some set of special characters, is equivalent toa = a <operator> b
only ifa
doesn't have the<operator>=
method, for example:特殊类:元组和符号
正如 Rahul G 提到的,元组和符号的语法稍微特殊。
'x
是Symbol("x")
的(p1,p2,..,pn)
是缩写对于案例类Tuplen[T1,T2,..,Tn](p1,p2,..,pn)
例如,以下两个是等效的。
Special Classes: Tuples and Symbols
As mentioned by Rahul G, tuples and symbols get a slightly special syntax.
'x
is short forSymbol("x")
(p1,p2,..,pn)
is short for a case classTuplen[T1,T2,..,Tn](p1,p2,..,pn)
For example, the following two are equivalent.
除了 Jaxkson 的答案之外:
type F[A,B]
可以用作AF B
。例如:
=>;方法定义中的 type
使编译器将表达式包装在函数 thunk 中的方法调用中。例如
In addition to Jaxkson's answer:
type F[A,B]
can be used asA F B
.For example:
=> type
in a method definition makes the compiler wrap expressions inside the method call in a function thunk.For example
提取器:
提取器有两种方法,
unapply
和unapplySeq
。它们用于多变量赋值和模式匹配。第一个用例是 unapply 获取它应该匹配的对象,并根据它是否匹配返回一个
Boolean
,例如,老实说,我不'并不能真正理解上述语法的目的,因为只需将代码放入
case
语句中即可轻松完成。当然,如果您有更好的示例,请在下面留言unapply的一般情况采用一些固定数量的参数并返回
Option[T]
对于单个参数或Option[(p1,p2,...)]
对于多个参数,即具有匹配值的元组,例如,从上面的代码继续:注意: 案例类执行所有这些
应用
/取消应用< /code> 为您定义(以及其他内容),因此请尽可能使用它们以节省时间并减少代码。
unapplySeq
。这与上面的unapply
类似,只是它必须返回某种序列的Option
。举个简单的例子,
Extractors:
There are two methods used for extractors,
unapply
andunapplySeq
. These are used in multiple variable assignments and pattern matching.The first use case is where unapply takes the object it is supposed to match and returns a
Boolean
based on whether or not it matches, for example,Honestly, I don't really get the purpose of the above syntax since it can be done almost just as easily by just putting the code in the
case
statements. Of course if you have a better example, leave a comment belowThe general case where
unapply
takes some fixed-number of parameters and returns either anOption[T]
for a single parameter or aOption[(p1,p2,...)]
for multiple, i.e. a Tuple with the matched values, for example, continuing from the above code:Note: Case classes do all those
apply
/unapply
definitions for you (as well as other stuff) so use them whenver possible to save time and reduce code.unapplySeq
. This works similarly tounapply
as above, except it must return anOption
of some kind of sequence.As a quick example,
匿名函数:
_ + _
是(a, b) => 的缩写。 a+b
Anonymous functions:
_ + _
is short for(a, b) => a + b
上下文将脱糖绑定到
隐式
参数中,例如考虑一个利用Monoid
类型类的函数:其中
:Monoid
部分是上下文绑定,获取翻译为:因此以下内容也可以编译:
Context bounds desugar into
implicit
parameters, e.g. consider a function that leverages theMonoid
type class:where the
: Monoid
part is a context bound, gets translated to:therefore the following compiles, too: