为什么我必须传递 new 关键字? [英] Why do I have to pass new keyword?

查看:61
本文介绍了为什么我必须传递 new 关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

val fsm = TestFSMRef(new SenderCollectorFsm)

并且不明白,为什么我必须将一个实例传递给 TestFSMRef.

And do not understand, why do I have to pass to TestFSMRef an instance.

我们来看看TestFSMRef:

object TestFSMRef {

  def apply[S, D, T <: Actor: ClassTag](
      factory: => T)(implicit ev: T <:< FSM[S, D], system: ActorSystem): TestFSMRef[S, D, T] = {
    val impl = system.asInstanceOf[ActorSystemImpl]
    new TestFSMRef(impl, Props(factory), impl.guardian.asInstanceOf[InternalActorRef], TestActorRef.randomName)
  }  

TActorClassTag 的子类型,但是如何知道,T 必须是一个目的?

T is subtype of Actor and ClassTag, but how to know, that T has to be an object?

推荐答案

Scala 是一种面向对象的语言.与几乎所有面向对象的语言一样,您只能将对象作为参数传递.此外,与大多数语言一样,类型不是对象.

Scala is an object-oriented language. Like in almost all object-oriented languages, you can only pass objects as arguments. Also, like most languages, types aren't objects.

所以,既然你只能传递对象,而类型不是对象,很明显你只能传递一个实例.

So, since you can only pass objects, and types aren't objects, it is clear that you can only pass an instance.

或者,更准确地说:存在两个独立的宇宙,类型的宇宙和的宇宙.在值的世界中,我们有一些方法将值作为圆括号(或偶尔花括号)中的参数.

Or, to be more precise: there exist two separate universes, the universe of types and the universe of values. In the universe of values, we have methods which take values as arguments in round parentheses (or occasionally curly braces).

在类型的世界中,我们有类型构造函数,它接受类型作为方括号中的参数.

In the universe of types, we have type constructors, which take types as arguments in square brackets.

两个宇宙恰好只有一个地方相遇,那就是路径依赖型.

There is exactly one place where the two universes meet, and that is in path-dependent types.

这篇关于为什么我必须传递 new 关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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