diff --git a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala index 952d5801e8..58470d9f45 100644 --- a/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala +++ b/akka-cluster-sharding-typed/src/main/scala/akka/cluster/sharding/typed/scaladsl/ClusterSharding.scala @@ -312,7 +312,7 @@ object StartEntity { * * Not for user extension. */ -@DoNotInherit trait EntityTypeKey[T] { +@DoNotInherit trait EntityTypeKey[-T] { /** * Name of the entity type. @@ -362,7 +362,7 @@ object EntityTypeKey { * [[ActorRef]] and watch it in case such notification is desired. * Not for user extension. */ -@DoNotInherit trait EntityRef[M] extends RecipientRef[M] { this: InternalRecipientRef[M] => +@DoNotInherit trait EntityRef[-M] extends RecipientRef[M] { this: InternalRecipientRef[M] => /** * Send a message to the entity referenced by this EntityRef using *at-most-once* diff --git a/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleSpec.scala b/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleSpec.scala index c80ac1ecc4..b3108ca6d5 100644 --- a/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleSpec.scala +++ b/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleSpec.scala @@ -65,17 +65,28 @@ class AccountExampleSpec extends ScalaTestWithActorTestKit(AccountExampleSpec.co probe.expectMessage(Confirmed) ref ! Withdraw(10, probe.ref) probe.expectMessage(Confirmed) + + // The same probe can be used with other commands too: + ref ! GetBalance(probe.ref) + probe.expectMessage(CurrentBalance(90)) } "reject Withdraw overdraft" in { + // AccountCommand[_] is the command type, but it should also be possible to narrow it to + // AccountCommand[OperationResult] val probe = createTestProbe[OperationResult]() - val ref = ClusterSharding(system).entityRefFor(AccountEntity.TypeKey, "3") + val ref = ClusterSharding(system).entityRefFor[AccountCommand[OperationResult]](AccountEntity.TypeKey, "3") ref ! CreateAccount(probe.ref) probe.expectMessage(Confirmed) ref ! Deposit(100, probe.ref) probe.expectMessage(Confirmed) ref ! Withdraw(110, probe.ref) probe.expectMessageType[Rejected] + + // ... thus restricting the entity ref from being sent other commands, e.g.: + // val probe2 = createTestProbe[CurrentBalance]() + // val msg = GetBalance(probe2.ref) + // ref ! msg // type mismatch: GetBalance NOT =:= AccountCommand[OperationResult] } "handle GetBalance" in {