Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

Initialization of variables explicitly declared as Hash[...] #5644

Open p6rt opened 8 years ago

p6rt commented 8 years ago

Migrated from rt.perl.org#129214 (status was 'new')

Searchable as RT129214$

p6rt commented 8 years ago

From roam@ringlet.net

Hi,

While trying to write a function returning Hash[Array[Str]], I came up against what looks like two related problems. All of this is with both 2016.08.1 and a current one - "This is Rakudo version 2016.08.1-116-gf648d3b built on MoarVM version 2016.08-35-g5108035".

First of all, if a variable is declared, but not initialized, it seems that some kind of initialization is delayed until an assignment is made and then a type check breaks. Here's an example​:

#!/usr/bin/env perl6

use v6.c;

my Hash[Int] $data;

say 'Created, not initialized yet'; dd $data;

say 'About to initialize it...'; $data\ = 5;

say 'We never get here, do we?'; dd $data;

The output is​:

[roam@​straylight ~/lang/perl/misc/rt/hash/01-init]$ perl6 hash-init.p6 Created, not initialized yet Hash[Int] $data = Hash[Int] About to initialize it... Type check failed in assignment to $data; expected Hash[Int] but got Hash (${})   in block \ at hash-init.p6 line 11

And this leads almost directly to the second problem. I tried to work around this message by initializing the hash at creation time, and this seems to actually lose the value type constraint! Here's an example​:

#!/usr/bin/env perl6

use v6.c;

my Hash[Int] $data .= new();

say 'Created and initialized'; dd $data;

say 'About to assign to it...'; $data\ = 3/5;

say 'So did we really store a Rat into a hash of Ints?'; dd $data; dd $data\;

And the output is​:

[roam@​straylight ~/lang/perl/misc/rt/hash/02-weird-types]$ perl6 hash-int-rat.p6 Created and initialized Hash[Int] $data = (my Int %) About to assign to it... So did we really store a Rat into a hash of Ints? Hash[Int] $data = (my Int % = :key(0.6)) Rat \ = 0.6

Thanks in advance for looking into this!

G'luck, Peter