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

Duplicate parameter names #4381

Closed
markknol opened this issue Jul 3, 2015 · 11 comments
Closed

Duplicate parameter names #4381

markknol opened this issue Jul 3, 2015 · 11 comments
Assignees

Comments

@markknol
Copy link
Member

markknol commented Jul 3, 2015

Is this expected behavior?

function func(a, a) trace(a);
func("Hello", "World");
// outputs "World"

(http://try.haxe.org/#bdE78)

In other languages you get an error like this: The parameter name 'a' is a duplicate.

@Simn
Copy link
Member

Simn commented Jul 3, 2015

That should surely be an error. I hope this is not a regression...

@back2dos
Copy link
Member

back2dos commented Jul 3, 2015

I think that has always been the case. Given that you can write var a = "Hello", a = "World"; I would say it is only consistent.

On Fri, Jul 3, 2015 at 1:56 PM, Simon Krajewski notifications@github.com
wrote:

That should surely be an error. I hope this is not a regression...


Reply to this email directly or view it on GitHub
#4381 (comment)
.

@Simn
Copy link
Member

Simn commented Jul 3, 2015

It would be cool if the second a could reference the first one though. :P

@markknol
Copy link
Member Author

markknol commented Jul 3, 2015

I think especially in case of parameters you just want an error.

@back2dos
Copy link
Member

back2dos commented Jul 3, 2015

@markknol I would speculate that it's meant to allow for function (_, _, theArgumentYouReallyWant) {}

@Simn It seems it can: http://try.haxe.org/#b94fE

Slightly off-topic: why was referencing previous variables in the same declaration allowed? I would say var x = y, y = x; is a very clear way to swap variables. And IIRC this was still possible a couple of years ago.

I see this:

{
   var a = exprA, b = exprB;
   somethingHere;
}

As this:

(function (a, b) {
   somethingHere;
})(exprA, exprB)

So I've always been puzzled by the fact that exprB may reference a. It feels uncomfortably close to JavaScript's weird scoping rules.

@markknol
Copy link
Member Author

markknol commented Jul 3, 2015

@back2dos Just curious; In which case would you define a function with those underscores?

@Simn
Copy link
Member

Simn commented Jul 3, 2015

I find it natural in a left-to-right kind of way. It's also trivial to generate it to our targets, whereas some swapping behavior would be tricky to get right.

@back2dos
Copy link
Member

back2dos commented Jul 4, 2015

@markknol Consider a map with the standard JavaScript signature. In that case you would write [1, 2, 3].map(function (x, _, _) return x + 2).

@back2dos
Copy link
Member

back2dos commented Jul 4, 2015

@Simn No doubt it is tricky, but you already got it right, given that this works already:

{
  var x = 5, y = 6;
  {
    var x = y;
  }
  {
    var y = x;
  }
}

Anyway, it's nothing we can change any time soon. This thread just reminded me of how odd I find it to be, so I had to ask ^^

@ncannasse
Copy link
Member

We could forbid same-function-parameter-name, with the exception of underscore, that sounds like a good middle ground

@Simn Simn self-assigned this Jul 21, 2015
@Simn
Copy link
Member

Simn commented Nov 19, 2015

Thinking about this now I don't think it's worth disallowing this. Who knows what crazy macro-generated functions might have duplicate argument names somewhere. I also don't think this would catch any programming errors.

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

4 participants