New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attribute type validation with slipped constructor #5849
Comments
From tim.bollman@live.comIf you create a typed array attribute for a class and then attempt to assign it in the constructor using a hash slip, it fails type validation. Error message: Type check failed in assignment to @!b; expected Str but got Array ($["a", "b", "c"]) use v6; plan 8; Thanks, Tim Bollman |
From @zoffixznetOn Fri, 02 Dec 2016 13:13:36 -0800, TimTom wrote:
This is the crux of the issue: dd [ | (b => []) ]; # [:b([])] A pair (slipped or not), does not itemize the array, so the array's values get assigned to the attribute, I don't know whether that's a bug or whether there's anything we can do about this behaviour. |
The RT System itself - Status changed from 'new' to 'open' |
From tim.bollman@live.comOn Fri, 02 Dec 2016 15:59:27 -0800, cpan@zoffix.com wrote:
Ah, I had thought the is($a.b, @b) would validate that the slipped version was assigning as a set of 3 elements. But if you add a check directly on .elems it returns 1. So at least the assignment is consistently using it as an array of one item, while I thought it was type-checking it one way and assigning as I would expect. I figured that |%args was the way to go because it's the natural way to adjust arguments in a factory constructor like the following. Where you check the arguments for which variant you want to build, adjust parameters accordingly (for example turning string name references into real references), then call the real object constructor. In particular, it prevents you from doing something like: sub build(*%args is copy) { Without the is copy, that passes type validation, except if you pass in something like b => @b, the assignment will modify the actual elements of @b which is not ideal in practice. You could instead do something like my @new-b = ...; A.new(|%args, \(b => @new-b)); which works as long you can quantify all the parameters you are adjusting, but as things get more complicated, that's not always possible. Expanded test case attached. |
From tim.bollman@live.comuse v6; use Test; class A { class A-validate { my @b = <a b c>; my %construct = ( plan +(A, A-validate) * %construct.elems * 3; for (A, A-validate) -> $type { |
Migrated from rt.perl.org#130246 (status was 'open')
Searchable as RT130246$
The text was updated successfully, but these errors were encountered: