|
|
Subscribe / Log in / New account

Opposition to Python type hints

Opposition to Python type hints

Posted May 7, 2015 16:51 UTC (Thu) by niner (subscriber, #26151)
In reply to: Opposition to Python type hints by ibukanov
Parent article: Opposition to Python type hints

Perl does check variable names at compile time in strict mode. The only exception is fully qualified package globals like $Foo::Bar::global_variable. But those are very rare.

Perl 6 throws errors for invalid method names if possible at compile time. I.e. when it knows about the types involved and no fallbacks are present. It's also very helpfully complaining about invalid arguments to methods and functions. And its type system allows very fine grained specification of types and signatures. E.g. it allows one to declare a function that takes a hash (dict in Python) as argument and requires that this hash contains certain keys. So in essence, it makes a combination of duck typing and static types possible and useful.


to post comments

Opposition to Python type hints

Posted May 7, 2015 17:42 UTC (Thu) by ibukanov (subscriber, #3942) [Link] (1 responses)

Hm, with perl 5.18.4:

~/s> cat x.pl
use strict;

my $a = 10;

if ($a) {
print "OK\n";
} else {
print $b . $b . "\n";
}
~/s> perl -w x.pl
OK

No warnings or errors about undeclared $b. What did I miss?

Opposition to Python type hints

Posted May 7, 2015 17:49 UTC (Thu) by niner (subscriber, #26151) [Link]

That $a and $b are predefined package globals in Perl 5. They should be used for sort blocks:

my @sorted = sort { $a <=> $b } @unsorted;

Try your example with $x and $y and you'll get:

~> perl x.pl
Global symbol "$y" requires explicit package name at x.pl line 6.
Global symbol "$y" requires explicit package name at x.pl line 6.
Execution of x.pl aborted due to compilation errors.

Yes, that's surprising behavior and it's one of the reasons for Perl 6 being a breaking change. Of course, $a and $b are not exactly good variable names anyway :)


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds