3 ms·
Is there a type-system that provides runtime safety? I thought type-erasure was common practice
by emoII 2y ago
Is there a type-system that provides runtime safety? I thought type-erasure was common practice
- daotoad 2y agoRaku has a really weird but cool type system[1] that does both compile time and runtime checks. Because some checks are expected to be runtime only, it lets you specify types like "Odd integers" by writing a `where` clause. ``` subset OddInteger of Int where !(* %% 2) ``` You can use multiple dispatch to separate dispatch from processing: ``` subset Fizz where * %% 3 subset Buzz where * %% 5 subset FizzBuzz where * %% 15 multi sub fizzbuzz(FizzBuzz $) { 'FizzBuzz' } multi sub fizzbuzz(Buzz $) { 'Buzz' } multi sub fizzbuzz(Fizz $) { 'Fizz' } multi sub fizzbuzz(Int $number) { $number } (1 .. 100)».&fizzbuzz.say; ``` Or even use inline anonymous subsets when you declare your functions: ``` multi sub fizzbuzz(Int $ where * %% 15) { 'FizzBuzz' } multi sub fizzbuzz(Int $ where * %% 5) { 'Buzz' } multi sub fizzbuzz(Int $ where * %% 3) { 'Fizz' } multi sub fizzbuzz(Int $number ) { $number } (1 .. 100)».&fizzbuzz.say; ``` Raku's type system is one of its features that will show you new ways of thinking about code. I advocate playing with Raku specifically for mind expansion because it has so many interesting ideas built in. [1] https://docs.raku.org/language/typesystem https://docs.raku.org/language/typesystem