alex / bagel

Bagels are delicious
BSD 3-Clause "New" or "Revised" License
22 stars 1 forks source link

Can you pattern match collection types? #31

Open dreid opened 10 years ago

dreid commented 10 years ago

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)
alex commented 10 years ago

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.