3 ms·
Interesting, is this a new-ish idea in type systems? The first time I saw something similar was in Kotlin as a technique for dealing with null checks without ha
by blacktriangle 5y ago
Interesting, is this a new-ish idea in type systems? The first time I saw something similar was in Kotlin as a technique for dealing with null checks without having to rename the unwrapped variable. Are there other languages that have this feature?
- milliams 5y agoI think that Python's type checkers do this sort of thing now, e.g. via TypeGuard (https://www.python.org/dev/peps/pep-0647/ https://www.python.org/dev/peps/pep-0647/)
- iso8859-1 5y agoThat's a PEP for Python 3.10, which isn't even released yet. And Mypy only supports it since May: https://github.com/python/mypy/issues/5206#issuecomment-844144556 https://github.com/python/mypy/issues/5206#issuecomment-8441...
- ajanuary 5y agoI first saw it in Typed Racket under the name "Occurrence typing" [1]. It's not a coincidence that Typed Racket is also a type system applied retroactively to a dynamically typed language. The first mention of "occurrence typing" in scheme I can find is a 2008 paper [2] [1] https://docs.racket-lang.org/ts-guide/occurrence-typing.html https://docs.racket-lang.org/ts-guide/occurrence-typing.html [2] https://www2.ccs.neu.edu/racket/pubs/popl08-thf.pdf https://www2.ccs.neu.edu/racket/pubs/popl08-thf.pdf
- muglug 5y agoTypeScript doesn't do proper occurrence typing: https://www.typescriptlang.org/play?ts=4.4.0-beta#code/GYVwdgxgLglg9mABMOcAUBDAXIgzlAJxjAHMAfMEAGyoBpEAjHfI0i6qgSmcOJMQDeAWABQicYhjBEmRAF4FiSjUQAyVY3mLlXQaIkHEBAKZQQBJAHIAJseAZqUSwG59EgL6i34qTIyIAQm0OTj0xQ3ETMwtEDFdw8U8RbyNTcyQGePcgA https://www.typescriptlang.org/play?ts=4.4.0-beta#code/GYVwd... Compare to Typed Racket: #lang typed/racket (: foo (-> (U String Null) (U String Null) String)) (define (foo a b) (cond [(and (null? a) (null? b)) "default"] [(not (null? a)) a] [#t b])) Or Psalm, a PHP typechecker I created with occurrence typing: https://psalm.dev/r/60ef4c299e https://psalm.dev/r/60ef4c299e