alex / alex/bagel

Can you pattern match collection types?

Ouverte
#31 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub
language-design
Langage dominant
Python
Étoiles
21
Forks
1
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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…

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

self.users.append(user)
```

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.