什么是“扩展{..}” Scala对象定义中的子句,没有超类名,吗? [英] What does the "extends {..}" clause in Scala object definition, without superclass name, do?

查看:183
本文介绍了什么是“扩展{..}” Scala对象定义中的子句,没有超类名,吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Scala编程中发现了这个代码示例,第2版。(第25章,清单25.11):

I found this code example in Programming in Scala, 2nd Ed. (Chapter 25, Listing 25.11):

object PrefixMap extends {
  def empty[T] = ...

  def apply[T](kvs: (String, T)*): PrefixMap[T] = ...

  ...
}

为什么是没有超类名称的 extends 子句?它看起来像扩展一个匿名类,但出于什么目的?随附的文字没有解释甚至在任何地方提到这种结构。代码实际上编译并显然在有或没有它的情况下都能正常工作。

Why is the extends clause there without a superclass name? It looks like extending an anonymous class, but for what purpose? The accompanying text doesn't explain or even mention this construct anywhere. The code actually compiles and apparently works perfectly with or without it.

OTOH我在几个网页上找到了完全相同的代码,包括这个(看起来像本书章节的原始版本)。我怀疑到现在这么多读者的雷达已经过了一个错字...所以我错过了什么?

OTOH I found the exact same code on several web pages, including this (which looks like the original version of the chapter in the book). I doubt that a typo could have passed below the radars of so many readers up to now... so am I missing something?

我试图谷歌它,但挣扎甚至为它找到合适的搜索条件。所以有人可以解释这个构造在Scala中是否具有名称和/或实际用途吗?

I tried to google it, but struggled even to find proper search terms for it. So could someone explain whether this construct has a name and/or practical use in Scala?

推荐答案

看起来像是一个打印错误给我。但它会起作用,这可能有助于隐藏它。

Looks like a print error to me. It will work all the same, though, which probably helped hide it all this time.

无论如何,该对象正在扩展结构类型,尽管它也可能是早期的初始化,如果你最后有 XXX 。 MMMMM。它看起来更像是早期初始化,没有任何类或特性可以在以后初始化,实际上...结构类型不包含代码,我认为。

Anyway, that object is extending a structural type, though it could also be an early initialization, if you had with XXX at the end. MMmmm. It looks more like an early initialization without any class or trait to be initialized later, actually... structure types do not contain code, I think.

这篇关于什么是“扩展{..}” Scala对象定义中的子句,没有超类名,吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆