Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

use strict hashpairs #281

@rurban

Description

@rurban

See #280.

For hash assignments we should restrict the mapwhile producer to accept only pairs, not arbitrary lists. Something like use strict 'hashpairs'.

mapwhile will get a private OPp_MAP_HASH flag and warn with "Odd number of map elements in hash assignment". Implemented the warning with 8f9bc43
and the strict 'hashpairs' errror with fb247f2.

Producing lists:
-e'sub x{$,"ouch"};%h=map{$=>x}(0..3);while (($k,$v)=each %h){print "$k=>$v, "}'
=> 3=>ouch, ouch=>3, 1=>ouch, 2=>2, 0=>0 (5 keys!)
Similar: -e'%h=map{$=>(0,1)}(0..3);while (($k,$v)=each %h){print "$k=>$v, "}'
=> 1=>3, 2=>0, 0=>1 (only 3 keys, see below why).
Because -e'@h=map{$
=>(0,1)}(0..3);print join" ",@h' => 0 0 1 1 0 1 2 0 1 3 0 1

-e'%h=map{$_}(0..3);while (($k,$v)=each %h){print "$k=>$v, "}'
=> 2=>3, 0=>1 (2 keys)

All but 0 and 2 elements could be ignored, and wrong (uneven) hash assignment needs to be warned with 'Odd number of elements in hash assignment'
Well, the last cases could be argued to be just %hash = @array splicing. The rhs list constructor should not care about the lhs hash.
But with mapwhile, padav and rv2av on the rhs and a hash on the lhs of non-scalar assignments we do care now with warnings misc and strict hashpairs.

perl6 does not warn, it throws:

    my %h=(0..2)
    Odd number of elements found where hash initializer expected

And there's also a
X::Pairup::OddNumber is Exception "Odd number of elements found for .pairup()

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions