Skip to content

pattern matching for receive messages #58

@JohnMurray

Description

@JohnMurray

#47 added support for runtime type inspection via Any casting. This means that the Actor::receive method can match against types it expects to be able to handle. However, this requires some really ugly code:

if let Some(str_msg) = msg.as_any().downcast_ref::<StringWrapper>() {
    println!("received message: {}", str_msg.value);
    ctx.send_message(ctx.sender(), "pong");
}

Ideally this should just be something similar to a match statement. However since we can't use an actual match statement, a match macro will have to do. I can imagine something like:

accept!(String, (message: String) -> {
  // String handler
});

The simplest thing to do might be to construct some sort of object that can process the input and return a result/match type that indicates whether or not the message was able to be processed.

Ideally these can be composed together (although that's not necessarily part of this PR) into behaviors:

behavior! {
    accept!(String,   (message: String)   -> { /* ... */ }),
    accept!(HelloMsg, (message: HelloMsg) -> { /* ... */ }),
    accept!(int,      (message: int)      -> { /* ... */ }),
}

In all of my examples above, I've also dropped the primitive wrapper types from the picture. I think these wrapper types should mostly be implemenation details. So whatever exists here should automatically unwrap the built-in primitive wrapper types.

  • Create accept! macro (name TBD)
  • Automatically unwrap primitive types

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

Status

Backlog

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions