Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Outlaw to declare a lexical twice in the same scope

4 views
Skip to first unread message

Steve Lukas

unread,
Sep 11, 2006, 7:35:08 AM9/11/06
to perl6-l...@perl.org
Hello,
perhaps I've missed a discussion about it, but I can't find a reason for a (IMHO infelicitous) specification. In S04 is said:

"If you declare a lexical twice in the same scope, it is the same lexical"

I would argue for: If you declare a lexical twice in the same scope, it is an error!

Well, this error happens most likely due to my tiredness and I want the compiler to wake me up.
This can be important because I would expect that
C< my $x = 7;>
does not change/overwrite the value of an existing $x. If I want to change the defined $x, I don't declare it again.

Does anybody agree?
Regards
Stefan



---------------------------------
Yahoo! Messenger with Voice. Make PC-to-Phone Calls to the US (and 30+ countries) for 2「/min or less.

Rick Delaney

unread,
Sep 11, 2006, 10:09:41 AM9/11/06
to Steve Lukas, perl6-l...@perl.org
On Mon, Sep 11, 2006 at 04:35:08AM -0700, Steve Lukas wrote:
>
> I would argue for: If you declare a lexical twice in the same scope, it is an error!
>
> Well, this error happens most likely due to my tiredness and I want the compiler to wake me up.
> This can be important because I would expect that
> C< my $x = 7;>
> does not change/overwrite the value of an existing $x. If I want to change the defined $x, I don't declare it again.
>
> Does anybody agree?

I don't. I often will do this during early development or debugging:

my $x = 'some initial state';
my $x = 'a different state';

I add the second line to try out a different code path. I do it this
way rather than just change the first line to the second because:

a) it's easier to restore the initialization back to its original state
if I change something else,
b) I won't forget to restore the initialization because I will get a
warning,
c) I can keep a bunch of different states in the file to try and just
have to cut-and-paste to try a new one, without troubling myself
with commenting out the unwanted states.

Presumably you will be able to make warnings fatal as in Perl 5 so you
should be able to get the behaviour you want, too.

--
Rick Delaney
ri...@bort.ca

Juerd

unread,
Sep 11, 2006, 12:33:00 PM9/11/06
to perl6-l...@perl.org
Steve Lukas skribis 2006-09-11 4:35 (-0700):

> "If you declare a lexical twice in the same scope, it is the same lexical"
> I would argue for: If you declare a lexical twice in the same scope,
> it is an error!

I agree.

The reason that I love "my $foo" is that it always gives me a new
variable. I can safely use this anywhere in the code, without any need
to read all the existing code. This is, for me, one of the most
important aspects of having lexicals in the language: I can add
(debugging or otherwise temporary) code to any existing project without
getting to know the structure of the project's code.

Perl 5 warns that a second declaration masks the first. This is fine: it
tells me about the stupid mistake I made and lets me fix it. A
compile error would be fine too. In fact, even better because then my
probably broken code isn't executed then.

Just ignoring the declaration is bad, just like implicit declaration. If
we do this, we get only typo checking, and none of the other nice
protection that lexical declaration gives us.
--
korajn salutojn,

juerd waalboer: perl hacker <ju...@juerd.nl> <http://juerd.nl/sig>
convolution: ict solutions and consultancy <sa...@convolution.nl>

cma...@gmail.com

unread,
Sep 11, 2006, 12:42:16 PM9/11/06
to perl6-l...@perl.org
Steve (>):

> "If you declare a lexical twice in the same scope, it is the same lexical"
>
> I would argue for: If you declare a lexical twice in the same scope, it is an error!

I agree.

Enforcing one and only one declaration feels like a Good Thing, for
Juerd's reasons. With me, multiple 'my' for the same variable is
usually a copy-and-paste error -- one that I would want to catch. So I
wouldn't mind getting a compile-time error, actually.

// masak

dgla...@gmail.com

unread,
Sep 16, 2006, 5:47:27 PM9/16/06
to
Rick Delaney wrote:
> On Mon, Sep 11, 2006 at 04:35:08AM -0700, Steve Lukas wrote:
> > Well, this error happens most likely due to my tiredness and I want the compiler to wake me up.
> > This can be important because I would expect that
> > C< my $x = 7;>
> > does not change/overwrite the value of an existing $x. If I want to change the defined $x, I don't declare it again.
> >
> > Does anybody agree?
>
> I don't. I often will do this during early development or debugging:
>
> my $x = 'some initial state';
> my $x = 'a different state';
>
> I add the second line to try out a different code path. I do it this
> way rather than just change the first line to the second because:

What's wrong with commenting out the first line?

--dave

0 new messages