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

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

Open
p6rt opened this issue Sep 6, 2016 · 1 comment
Open

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

p6rt opened this issue Sep 6, 2016 · 1 comment

Comments

@p6rt
Copy link

p6rt commented Sep 6, 2016

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

Searchable as RT129214$

@p6rt
Copy link
Author

p6rt commented Sep 6, 2016

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<key> = 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 <unit> 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<key> = 3/5;

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

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 <element> = 0.6

Thanks in advance for looking into this!

G'luck,
Peter

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