perl11 / cperl

A perl5 with classes, types, compilable, company friendly, security
http://perl11.org/
Other
142 stars 17 forks source link

Mu::new fields (args) not typechecked #387

Closed bbkr closed 5 years ago

bbkr commented 6 years ago

Following this blog post http://perl11.org/blog/cperl-classes.html on cperl v5.28.1c I get weird results:

$ cperl -e 'class Foo { has int $bar }; my $foo = Foo->new( bar => "baz" ); print $foo->bar'
bar

Should I use any special syntax (like :$bar) to indicate that param is named, not positional? Why attribute with int type accepted string value?

Thanks for clarification.

rurban commented 6 years ago

No, cperl does not have support for named params yet, sorry. It's on my todo list, but not high prio.

Why does it return bar? With my $foo = Foo->new( 1 ) it returns 1. Looks like the bar argument is converted to a bareword string. Any bareword is accepted, not just a known field.

perl -Dxto -e'sub Foo::new {1} ; new Foo( xxx );'

So that's that. Certainly weird, but consistent. Not only method specific, but for all subs. Almost like PHP. Only caught with use strict.

Why did it pass the typechecker? A bug! Thanks. Should have been caught even at compile-time. But given that's a MOP call (Mu::new) it's more like a limitation. At least a runtime typecheck should have caught that. Will fix both cases.

bbkr commented 6 years ago

Thanks for explanation!

rurban commented 5 years ago

I implemented now the run-time typecheck for Mu->new and pp_signature (all pure-perl calls), but found out that role composition (class does) does not fixup the stash of the new methods/roles yet. The typechecker found this bug. See branch bugfix/gh387-typecheck-munew