alex / alex/bagel

Can you pattern match collection types?

未關閉
#31 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
language-design
主要語言
Python
星號
21
分支
1
PR 合併指標
30 天內沒有已合併 PR

描述

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)
```

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。