Skip to content
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

Rakudo can't autovivify arrays from hash items via push() #2025

Closed
p6rt opened this issue Aug 6, 2010 · 8 comments
Closed

Rakudo can't autovivify arrays from hash items via push() #2025

p6rt opened this issue Aug 6, 2010 · 8 comments

Comments

@p6rt
Copy link

p6rt commented Aug 6, 2010

Migrated from rt.perl.org#77038 (status was 'resolved')

Searchable as RT77038$

@p6rt
Copy link
Author

p6rt commented Aug 6, 2010

From @moritz

07​:45 < moritz_> rakudo​: my %h; push %h<a>, 1, 2; say %h.perl
07​:45 <+p6eval> rakudo 19931f​: OUTPUT«Method '!fill' not found for
invocant of class ''␤ in 'List​::push' at line 2610​:CORE.setting␤ in
main program body at line 22​:/tmp/IxHzXM4aK0␤»

@p6rt
Copy link
Author

p6rt commented Aug 7, 2010

From @Timbus

The issue isn't with hashes, it's with the Any that is returned when you
access a key that doesn't exist.
Any doesn't implement .push or !fill, and the global push sub is
actually List.push, which obviously expects a List. Pmichaud suggested
the fix​:
<pmichaud> Any implements .push.
<pmichaud> but you have to check for definedness, too.
<pmichaud> If defined, then nextsame.

This patch seems to comply. And if push is meant to vivify an Array
instead of a List, it can be easily changed.

@p6rt
Copy link
Author

p6rt commented Aug 7, 2010

From @Timbus

pushfix.patch
diff --git a/src/core/Any-list.pm b/src/core/Any-list.pm
index f61784e..6e4175f 100644
--- a/src/core/Any-list.pm
+++ b/src/core/Any-list.pm
@@ -26,6 +26,11 @@ augment class Any {
         ~pir::join__SsP($separator, self.flat.eager);
     }
 
+    our multi method push (*@items) is export {
+        if self //= List.new() { nextsame }
+        else { self.push(@items) }
+    }
+
     multi method elems() {
         1;
     }
diff --git a/src/core/List.pm b/src/core/List.pm
index ea699d7..a8753a4 100644
--- a/src/core/List.pm
+++ b/src/core/List.pm
@@ -80,7 +80,7 @@ augment class List does Positional {
                   !! fail('Undefined value popped from empty array');
     }
 
-    our method push(*@values) is export {
+    our method push(*@values) {
         self!fill(0);
         pir::push(@!rest, @values.Seq.eager.iterator );
         self;

@p6rt
Copy link
Author

p6rt commented Aug 7, 2010

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Sep 2, 2010

From @masak

<mfollett> rakudo​: my %hash; push(%hash<books>, 'Cooking for Geeks');
<p6eval> rakudo d75c61​: OUTPUT«Method '!fill' not found for invocant
of class '' [...]
* masak submits rakudobug
<mfollett> Anyone know what I'm doing wrong there?
<masak> mfollett​: looks good to me.
<mfollett> masak​: ok, good, I thought I was crazy
<masak> mfollett​: as a general rule, you're not. :)

@p6rt
Copy link
Author

p6rt commented Oct 22, 2011

From @coke

No change in the past year.

--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented May 29, 2012

From @moritz

fixed now, and tested in S02-types/autovivification.t

@p6rt
Copy link
Author

p6rt commented May 29, 2012

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed May 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant