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

LTA error message when redeclaring a symbol with different type #4522

Closed
p6rt opened this issue Sep 7, 2015 · 4 comments
Closed

LTA error message when redeclaring a symbol with different type #4522

p6rt opened this issue Sep 7, 2015 · 4 comments

Comments

@p6rt
Copy link

p6rt commented Sep 7, 2015

Migrated from rt.perl.org#126008 (status was 'rejected')

Searchable as RT126008$

@p6rt
Copy link
Author

p6rt commented Sep 7, 2015

From @AlexDaniel

Code​:
my Int $x = 5;
my Str $x = 'hello';

Result​:
Potential difficulties​:
  Redeclaration of symbol $x
  at ./test.pl​:3
  ------> my Str $x⏏ = 'hello';
Type check failed in assignment to '$x'; expected 'Str' but got 'Int'
  in block <unit> at ./test.pl​:2

While compile-time “potential difficulties” part totally makes sense,
run-time error is confusing. “got 'Int'” is weird, because I gave it a Str.

You might think that using an integer can help​:
my Int $x = 5;
my Str $x = 10;

Result​:
Potential difficulties​:
  Redeclaration of symbol $x
  at ./test.pl​:3
  ------> my Str $x⏏ = 10;
Type check failed in assignment to '$x'; expected 'Str' but got 'Int'
  in block <unit> at ./test.pl​:2

In other words, it is exactly the same error… And not only you cannot
assign anything, but you cannot even declare it​:
my Int $x = 5;
my Str $x;

Result​:
Potential difficulties​:
  Redeclaration of symbol $x
  at ./test.pl​:3
  ------> my Str $x⏏;
Type check failed in assignment to '$x'; expected 'Str' but got 'Int'
  in block <unit> at ./test.pl​:2

It looks totally less than awesome for me. Interestingly, if it was a
compile-time death, it would have been less LTA.

@p6rt
Copy link
Author

p6rt commented Sep 7, 2015

From @moritz

On Sun Sep 06 20​:58​:11 2015, alex.jakimenko@​gmail.com wrote​:

Code​:
my Int $x = 5;
my Str $x = 'hello';

Result​:
Potential difficulties​:
Redeclaration of symbol $x
at ./test.pl​:3
------> my Str $x⏏ = 'hello';
Type check failed in assignment to '$x'; expected 'Str' but got 'Int'
in block <unit> at ./test.pl​:2

You should note that the line "my Str $x = 'hello';" is actually line 3, so here it complains about the line where you assign the integer.

While compile-time “potential difficulties” part totally makes sense,
run-time error is confusing. “got 'Int'” is weird, because I gave it a Str.

No, you gave it an Int. You are just confused about your line numbers.

Note that redeclarations actually replace the previous declaration, at compile time. So at the time the initialization of the first declaration of $x is run, its type has already changed to Str.

So

my Int $x = "a";
my Str $x = "b";
say $x # warns and prints b\n

Yes, this is surprising, which is why we emit a warning. Otherwise, the behavior seems correct.

@p6rt
Copy link
Author

p6rt commented Sep 7, 2015

The RT System itself - Status changed from 'new' to 'open'

@p6rt p6rt closed this as completed Sep 7, 2015
@p6rt
Copy link
Author

p6rt commented Sep 7, 2015

@moritz - Status changed from 'open' to 'rejected'

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