4 ms·
Do you think optional typing could work if it was part of the language? Or the problems with it are intrinsic?
by swah 11y ago
Do you think optional typing could work if it was part of the language? Or the problems with it are intrinsic?
- Ovid 11y agoWhen it's native to the language, it's awesome. When it's bolted on as an afterthought, you get articles like this. Native gradual typing can be much faster and assuming the language has a decent test suite, cases can quickly be added to uncover those troublesome spots. Aftermarket gradual typing leads to a lot of information that you could use to significantly improve the performance of the language (hey, we have an int, so let's use native types instead of our language's version), but because they're not native, that typically doesn't happen.
- grandalf 11y agowhat do you think is the best implementation of optional typing in a language?
- Ovid 11y agoI can't say, but I love what Perl 6 has done. For example, here's a Point class, with read/write, mandatory attributes: class Point { has $.x is rw is required; has $.y is rw is required; } Like many dynamic languages, there's nothing to stop you from assigning the string "foo" to one of those attributes, so let's fix that, but go even further and constrain the X and Y coordinates from -10.0 to 10.0. (Note that a "Rat" is a rational number, so it's perfectly precise and doesn't suffer from floating points imprecision): class Point { subset PointLimit of Rat where -10.0 .. 10.0; has PointLimit $.x is rw is required; has PointLimit $.y is rw is required; } So what that's done is allowed the dev to declare a new type, PointLimit, on the fly. It must be a number from -10 to 10. And then we have our X and Y attributes: my $point = Point.new( x => -1.2, y => 7.3 ); $point.x = 3.14; $point.y = -22.0; # Boom! Constraint violation
- jerf 11y agoIt would certainly be better if it were in there from the beginning. There's a set of things that are very hard to bolt on after-the-fact that aren't necessarily that expensive if you start with them. This seems to be one of them.