bagel icon indicating copy to clipboard operation
bagel copied to clipboard

Can you pattern match collection types?

Open dreid opened this issue 9 years ago • 1 comments

In Erlang it often ends up being useful to model the state invariants as pattern matches of expectations of complex types.

For instance, an IRC chat room that is empty the first user to join it becomes the operator, if it is ever empty again the new first user to join it becomes the operator.

You could explicitly track the empty state, but it is much easier to have the function that decides if it should op the joining user to simply dispatch on a match of the user list.

In bagel I'd expect this to look something like…

class Chatroom:
    users = List<User>
    operator = Option(User)
    ...
    def user_joining(self, user: User):
       match self.users:
            as []:
                self.operator = Some(user)
            as users:
                pass

       self.users.append(user)

dreid avatar Sep 24 '14 05:09 dreid

Well, besides that you're mutating immutable structures all over the place ;-)

I'm not sure about pattern matching data structures, this builds towards how do you pattern match against user defined types, and I don't know what that API looks like.

alex avatar Sep 24 '14 15:09 alex