Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The equivalent in Python is:-

  from typing import NewType
  
  UserId = NewType("UserId", int)


Actually, not really. In this case UserId is still an integer, which means any method that takes an integer can also take a UserId. Which means your co-workers are likely to just use integer out of habit.

Also, you can still do integer things with them, such as

> nonsense = UserId(1) + UserId(2)


It will not stop you doing this at runtime of course. It is only intended for type checking.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: