2

假设我有这两个类:

class Person (val name: String, val surname: String)

class Family (val nameFamily: String, val members: Set[Person])

现在在 main 方法中实例化两个类,如下所示:

val F1 = new Family ("Red", Set(P1, P2))
val P1 = new Person ("John", "Smith")
val P2 = new Person ("Luis", "Smith")

main 方法允许我在实例化之前输入家庭成员。我想要这个作为我的模型。但是如果我在创建之前输入成员,当我去写:

println(F1.members)

我返回一个集合(空)。

如果你先写主要人物,像这样:

val P1 = new Person ("John", "Smith")
val P2 = new Person ("Luis", "Smith")  
val F1 = new Family ("Red", Set(P1, P2))

我没有问题。

但我想以任何顺序编写实例,最后运行家庭验证。

我可以解决这个问题。我的意思是,我可以使用稍后创建的实例来初始化我的字段。

请原谅我糟糕的英文翻译。

上传#1

我已经在 Scala 中实现了一个域,我使用 DSL 创建了域的实例。我的 DSL 允许我以混合顺序实例化类。例如,我创建一个 Component,然后在这个 Component 中添加一些 Type。然后我创建我添加到组件的类型。在主要方法中我可以做到。作为主体的最后一条语句,我进行了验证。开始验证时,Type in the Component 没有找到任何东西,因为这些是稍后实例化的。这个问题只能用懒惰的方式解决吗?或者在域级别是否有解决方案。

4

2 回答 2

7

您可以使用lazy val

scala> :paste
// Entering paste mode (ctrl-D to finish)

case class Person (val name: String, val surname: String)
case class Family (val nameFamily: String, val members: Set[Person])

lazy val f1 = new Family ("Red", Set(p1, p2))
lazy val p1 = new Person ("John", "Smith")
lazy val p2 = new Person ("Luis", "Smith")

// Exiting paste mode, now interpreting.

defined class Person
defined class Family
f1: Family = <lazy>
p1: Person = <lazy>
p2: Person = <lazy>

scala> f1
res0: Family = Family(Red,Set(Person(John,Smith), Person(Luis,Smith)))

如果您只想在创建对象后在某处设置一些值,则应使用 scala 2.10 中的 Promise:

import concurrent.{Promise, Future}

case class Person (val name: String, val surname: String)
class Family (val nameFamily: String) {
  private val membersPromice = Promise[Set[Person]]
  def setMembers(m: Set[Person]) { membersPromice.success(m) }
  val membersFuture = membersPromice.future
  def members = membersFuture.value.map(_.get)
  override def toString() = "Family(" + nameFamily + ", " + members.getOrElse("<future>") + ")"
}

用法:

scala> val f = new Family("red")
f: Family = Family(red, <future>)

scala> val (p1, p2) = (Person("John", "Smith"), Person("Luis", "Smith"))
p1: Person = Person(John,Smith)
p2: Person = Person(Luis,Smith)

scala> f.setMembers(Set(p1, p2))

scala> f
res1: Family = Family(red, Set(Person(John,Smith), Person(Luis,Smith)))

scala> f.setMembers(Set(p1, p2))
java.lang.IllegalStateException: Promise already completed.

您可以使用类型安全的构建器模式

case class Person (val name: String, val surname: String)
case class Family (val nameFamily: String, val members: Set[Person])

sealed abstract class TBool
class TTrue extends TBool
class TFalse extends TBool
class FamilyBuilder[WithName <: TBool, WithMembers <: TBool](val nameFamily: Option[String], val members: Option[Set[Person]]) {
  def withName(nameFamily: String)(implicit i: WithName =:= TFalse) = new  FamilyBuilder[TTrue, WithMembers](Some(nameFamily), members)
  def withMembers(members: Set[Person])(implicit i: WithMembers =:= TFalse) = new  FamilyBuilder[WithName, TTrue](nameFamily, Some(members))
  def create()(implicit i1: WithName =:= TTrue, i2: WithMembers =:= TTrue) = Family(nameFamily.get, members.get)
}
object FamilyBuilder {
  def apply() = new FamilyBuilder[TFalse, TFalse](None, None)
}

用法:

scala> FamilyBuilder() withName "Red" withMembers Set(p1, p2) create()
res1: Family = Family(Red,Set(Person(John,Smith), Person(Luis,Smith)))

scala> FamilyBuilder() withMembers Set(p1, p2) withName "Red" create()
res2: Family = Family(Red,Set(Person(John,Smith), Person(Luis,Smith)))

scala> FamilyBuilder() withName "Red" create()
<console>:1: error: Cannot prove that TFalse =:= TTrue.
              FamilyBuilder() withName "Red" create()
                                             ^
于 2012-12-07T11:07:00.493 回答
0

你只需要让 people 变量变得懒惰

val F1 = new Family ("Red", Set(P1, P2))
lazy val P1 = new Person ("John", "Smith")
lazy val P2 = new Person ("Luis", "Smith")

scala> println(F1.members)
Set($line1.$read$$iw$$iw$Person@185623a7, $line1.$read$$iw$$iw$Person@3f3eb56c)

已编辑

您可以在类本身中将 Family 成员定义为惰性

class Family(val nameFamily: String, memberSet: => Set[Person]) {
    lazy val members= memberSet
}

这样,members属性仅根据需要进行评估

于 2012-12-07T11:07:02.273 回答