斯卡拉案例类“明确地暴露国家” [英] Scala case class "explicitly exposing the state"
问题描述
在阅读 akka文档正确的同时在消息和不变性部分下,它提到了案例类内部的显式暴露状态。所以我的问题是;
While reading akka docs right under messages and immutability section, It mentions about "explicitly exposing the state" inside case class. So my questions are;
对案例类说显式暴露国家是什么意思?
What is meant by saying "explicitly exposing the state" for case class?
为了实现不变性,为一个类写 case还不够吗?
还是我应该谨慎使用它?
In order to achieve immutability, isn't it enough to write "case" for a class? Or I should be careful about its usage ?
推荐答案
对案例类说显式暴露状态是什么意思?
What is meant by saying "explicitly exposing the state" for case class?
下面的actor用可变的 Set [Int]
表示其状态其值分别为 1
, 2
和 3
:
The actor below represents its state with a mutable Set[Int]
, which is initialized with the values 1
, 2
, and 3
:
case class State(s: mutable.Set[Int])
case class Add(num: Int)
case class Remove(num: Int)
class MyActor extends Actor {
val state = mutable.Set(1, 2, 3)
def receive = {
case GetState =>
sender ! State(state)
case Add(i) =>
state += i
case Remove(i) =>
state -= i
}
}
当此演员接收到一条 GetState
消息,它将其状态包装在 State
案例类中,并将其发送给发送者。即使 State
案例类是不可变的,其参数 s
也是可变的 Set
。因此,当 MyActor
创建具有状态的 State
实例并将其作为消息发送给<$的发送者时c $ c> GetState 消息, MyActor
的状态可以在 MyActor $ c $的边界之外进行修改c>本身。为了具体化,假设
另一个Actor
向 MyActor $ c发送一条
GetState
消息$ c>,此时 MyActor
将其状态发送到另一个Actor
。这是后者的参与者:
When this actor receives a GetState
message, it wraps its state in the State
case class and sends that to the sender. Even though the State
case class is immutable, its parameter s
is a mutable Set
. Therefore, when MyActor
creates a State
instance with its state and sends that as a message to the sender of the GetState
message, MyActor
's state becomes modifiable outside of the boundaries of MyActor
itself. To make this concrete, let's say AnotherActor
sends a GetState
message to MyActor
, at which point MyActor
sends its state to AnotherActor
. Here is the latter actor:
class AnotherActor extends Actor {
def receive =>
case State(state) =>
// MyActor's state is exposed here
state -= 2
}
AnotherActor
通过删除 2
MyActor 的状态
AnotherActor
modifies MyActor
's state by removing 2
from it, even though that state is delivered inside a case class.
要减轻这种泄漏,请将可变性限制在参与者本身上。在此示例中,定义 var state = immutable.Set()而不是
: val state = mutable.Set(1、2、3)
1,2,3)
To mitigate this sort of leak, restrict mutability to the actor itself. In this example, instead of having a val state = mutable.Set(1, 2, 3)
, define a var state = immutable.Set(1, 2, 3)
:
class MyActor extends Actor {
var state = immutable.Set(1, 2, 3)
def receive = {
case GetState =>
sender ! state
case Add(i) =>
state = state + i
case Remove(i) =>
state = state - i
}
}
这里, MyActor
可以安全地将其状态作为消息发送,因为它是不可变的 Set
(我们可以包装在案例类中设置
,但这不是必需的。)
Here, MyActor
can safely send its state as a message because it's an immutable Set
(we could wrap the Set
inside a case class, but that's not essential in this case).
为了实现不变性,为一个类写 case还不够吗?
In order to achieve immutability, isn't it enough to write "case" for a class?
否。在使用case类进行参与者消息传递时,请确保该类的所有参数本身都是不可变的。
No. When using a case class for actor messaging, ensure that all of the class's parameters are themselves immutable.
或者我应该谨慎使用它?
Or I should be careful about its usage?
是。
这篇关于斯卡拉案例类“明确地暴露国家”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!