develooper Front page | perl.perl6.users | Postings from December 2018

Basic question about lexical binding in relationship with "listassignment"

Thread Next
From:
Raymond Dresens
Date:
December 27, 2018 08:29
Subject:
Basic question about lexical binding in relationship with "listassignment"
Message ID:
CALBLdeYa46-2hMr74ALboSMkcMrDzOq_LZjuc_hEXgTV9a-JJQ@mail.gmail.com
Hello,

I'm getting up to speed with Perl 6 again after a long while, and I more
or less 'hit my nose' against something that I can reduce to the
following very basic snippet of code:

    my $foo = 3;
    say $foo;

    {
        say $foo;

        my $foo = 6;

        say $foo;
    }

This will not compile with Perl 6 (Rakudo 2018.12) -- lexical symbol
'$foo' is already bound to an outer symbol; the implicit outer binding
must be rewritten as OUTER::<$foo> before you can unambiguously declare
a new '$foo' in this scope.

This will compile with Perl 5 when I add 'use v5.14;' at the top and it
will then print 3, then 3 then 6 (as expected).

Well, it seems that I can 'cheat' by simply doing a list assignment:

    my $foo = 3;
    say $foo;

    {
        say $foo;

        my ($foo) = 6; # avoid `... '$foo' is already bound ...'

        say $foo;
        say $foo.WHAT; # is it really an integer?
    }

The output is:

    3
    (Any)
    6
    (Int)

This behavior is fine I think (but a little unexpected due to my
experience with Perl 5).

Is this behavior normal/wanted/intented (from a language/compiler
perspective?).

Perhaps this shouldn't be and the compiler should also complain in this
list assignment case, or perhaps generate a warning?

I'm not that confident to state that this is a bug ;)

Hence the reason why I kind-of report this,

I hope this is somewhat useful,

Thanks for your time,

Regards,

Raymond Dresens.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About