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

Re: Basic question about lexical binding in relationship with "listassignment"

Thread Previous | Thread Next
From:
Elizabeth Mattijsen
Date:
December 27, 2018 11:31
Subject:
Re: Basic question about lexical binding in relationship with "listassignment"
Message ID:
7DCA952D-3063-47FC-A043-2BEC8E811D20@dijkmat.nl
I’d say it is a bug you do not get the error with “my ($foo) = 6”, as clearly the first “say $foo” inside the scope is referring to the inner $foo, not the outer $foo.  So I’d suggest filing an issue for that: https://github.com/rakudo/rakudo/issues

> On 27 Dec 2018, at 09:29, Raymond Dresens <raymond.dresens@gmail.com> wrote:
> 
> 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 Previous | 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