Preferences

> I have worked on multiple 500K+ line Python projects.

There were already several 1M+ Perl LoC code bases by the end of the 1990s, as Perl use exploded at places like Amazon.

One key driver of Raku's design was making it easy to write large, clean, readable code bases, and easy to enforce coding policies if and when a dev team leader chose to.

(Of course, as with any PL, you can write messy unreadable code -- and you can even write articles and record videos that make it seem like a given PL is only for Gremlins. But it's so easy to write clean readable code, why wouldn't you do that instead?)

> type hint ... is never enough if it isn't guaranteed by a compiler or at runtime

Indeed. Type hints are a band aid.

Raku doesn't do hints. Raku does static first gradual typing.[1]

The "static first" means static typing is the foundation.

The "gradual typing" means you don't have to explicitly specify types, but if you do types, they're always enforced.

Enforcement is at compile time if the compiler's static analysis is good enough, and otherwise at run time at the latest.

For example, in the Gremlins article only one example uses a static type (`Int`). But user code can and routinely does include types, and they are enforced. For example:

    sub double(Int $x) { $x * 2 }
    double '42'
results in a compile time error:

    SORRY! Error while compiling ...

    Calling double(Str) will never work with declared signature (Int $x)
        (Int $x)
> total mess due to weak typing

I know what you mean, but to be usefully pedantic, the CS meaning of "weak typing" can be fully consistent with excellent typing discipline. Let me demonstrate with Raku:

    sub date-range(Date $from, Date $to) { $from..$to }
The subroutine (function) declaration is strongly typed. So this code...

    say date-range '2000-01-01', '2023-08-21';
...fails at compile time.

But it passes and works at run time if the function declaration is changed to:

    sub date-range(Date() $from, Date() $to) { $from..$to }
(I've changed the type constraints from `Date` to `Date()`.)

The changed declaration works because I've changed the function to be weakly typed, and the `Date` type happens to declare an ISO 8601 date string format coercion.

But what if you wanted to insist that the argument is of a particular type, not one that just so happens to have a `Date` coercion? You can tighten things up...

    sub date-range(Date(Str) $from, Date(Str) $to) { $from..$to }
...and now the argument has to already be a `Date` (or sub-class thereof), or, if not, a `Str` (Raku's standard boxed string type), or sub-class thereof, that successfully coerces according to `Date`'s ISO 8601 coercion for `Str`s.

And to finish that last bit, if an argument of a call is not a `Date` or `Str`:

* The compiler may reject the code at compile time for failing to type check; and, if it doesn't:

* The compiler will reject the code at run time for failing to type check unless it's either a `Date`, or a `Str` that successfully coerces to a `Date`.

This item has no comments currently.

Keyboard Shortcuts

Story Lists

j
Next story
k
Previous story
Shift+j
Last story
Shift+k
First story
o Enter
Go to story URL
c
Go to comments
u
Go to author

Navigation

Shift+t
Go to top stories
Shift+n
Go to new stories
Shift+b
Go to best stories
Shift+a
Go to Ask HN
Shift+s
Go to Show HN

Miscellaneous

?
Show this modal