Scala 类型关键字:如何最好地跨多个类使用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7441277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:28:13  来源:igfitidea点击:

Scala type keyword: how best to use it across multiple classes

scalatypes

提问by Alex Dean

Coming back to Scala after a spell writing Haskell, I've started using the type keyword to make my class definitions a bit more readable, eg:

在写完 Haskell 之后回到 Scala,我开始使用 type 关键字来使我的类定义更具可读性,例如:

type RestfulParams = Map[String, String]
def canonicalize(params: RestfulParams): String = { ... }

The trouble I've run into is that these type definitions need to live inside a class or object - they're not "first class citizens" like they are in Haskell. If I try to define a type outside of a class or object, I get a compiler expected class or object definition.

我遇到的麻烦是这些类型定义需要存在于类或对象中——它们不像在 Haskell 中那样是“一等公民”。如果我尝试在类或对象之外定义类型,则会得到一个 compiler expected class or object definition

My problem then is how to use these types across multiple classes and objects in a package? What I've done now seems quite ugly:

我的问题是如何在包中的多个类和对象中使用这些类型?我现在所做的看起来很丑陋:

object RestfulTypes { type RestfulParams = Map[String, String] etc }

And then in another class file:

然后在另一个类文件中:

import utils.{RestfulTypes => RT}
def get(resource: String, params: RT.RestfulParams): String = { ... }

Is there a nicer way of doing this - and incidentally do the Scala gurus think it's a good thing or a bad thing that types can only be defined inside classes/objects?

有没有更好的方法来做到这一点 - 顺便说一句,Scala 大师认为类型只能在类/对象中定义是好事还是坏事?

采纳答案by Ray Toal

Will package objectswork for you?

包对象对你的工作?

From the article:

从文章:

Until 2.8, the only things you could put in a package were classes, traits, and standalone objects. These are by far the most common definitions that are placed at the top level of a package, but Scala 2.8 doesn't limit you to just those. Any kind of definition that you can put inside a class, you can also put at the top level of a package. If you have some helper method you'd like to be in scope for an entire package, go ahead and put it right at the top level of the package. To do so, you put the definitions in a package object. Each package is allowed to have one package object. Any definitions placed in a package object are considered members of the package itself.

在 2.8 之前,您只能放入包中的内容是类、特征和独立对象。这些是目前最常见的位于包顶层的定义,但 Scala 2.8 并不仅限于这些定义。任何可以放在类中的定义,也可以放在包的顶层。如果你有一些辅助方法,你想在整个包的范围内,继续把它放在包的顶层。为此,您将定义放在包对象中。每个包允许有一个包对象。放置在包对象中的任何定义都被视为包本身的成员。

The package object scalahas many types and values already, so I think you can use the same technique for your own types.

包对象的Scala有许多类型和值了,所以我认为你可以使用同样的技术为自己的类型。

回答by Kim Stebel

I'm not sure what constitutes niceness in this case, but here are two options:

我不确定在这种情况下什么是友善,但这里有两个选项:

Using a trait: (Scala's Selfless Trait Pattern)

使用特质:(Scala 的无私特质模式

trait RestfulTypes {
  type Params = Map[String,String]
  //...
}

object MyRestService extends RestService with RestfulTypes { /* ... */ }

Or just import the object's members, effectively using it like a package:

或者只是导入对象的成员,像包一样有效地使用它:

import util.RestfulTypes._

回答by firephil

Youn can use a package object and put the type declaration in there. When you import that package the type definition will be available.

您可以使用包对象并将类型声明放在那里。当您导入该包时,类型定义将可用。

An example :

一个例子 :

//package myType
package object myType {type mine = List[String]}

Notice that the package object has the same name as the package in order to be usable without a object prefix i.e myType.mine If you name it with a different name for example :

请注意,包对象与包具有相同的名称,以便在没有对象前缀的情况下可用,即 myType.mine 如果您使用不同的名称命名它,例如:

//package myType
package object mainType {type mine = List[String]}

it can be accessed with mainType.mine when you import "package"

导入“包”时可以使用 mainType.mine 访问它

for more info : http://www.scala-lang.org/docu/files/packageobjects/packageobjects.html

更多信息:http: //www.scala-lang.org/docu/files/packageobjects/packageobjects.html