Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> There is also the issue that, please correct me if I got this wrong, the label that's in the signature becomes the name of the parameter inside the function. And as far as I can tell it cannot be changed/mapped.

You got that wrong. You can specify a separate internal name like this:

    foo(externalName internalName: Type) { ... }
> you actually need labels in your function declaration even for parameters that are unlabeled. Wut? And because of this, you need another special bit of syntax that says "despite the fact that I am now about to give a label, this is unlabeled"

I mean you need to refer to those values inside your function as something and that has to be specified somewhere. How else would you do it?

Also the `_` you're complaining about is just a special case of the syntax I described above. This just means the external parameter name is omitted at the call site. It's quite consistent: if two names are given, one is external and one is internal. If only one is given, that means the external and internal names are the same (which is convenient since this is the common case).

> It also means that a name that is actually purely internal to the function is part of the signature. Wat?

Again, where on earth else would you put it? That's only also half true: the public signature does not include the internal parameter name. For instance I can declare a protocol like this:

    protocol P {
        func foo(arg: Int)
    }
And both of these implementations will conform to it just fine despite the fact that the internal argument names are different:

    struct A: P {
        func foo(arg internalA: Int) { ... }
    }

    struct B: P {
        func foo(arg internalB: Int) { ... }
    }
> And sometimes you get partial labels that just kind of sit there.

Can you give an example? I'm not quite sure what you're talking about.



> You got that wrong.

Ahh, my bad! But only the "cannot be changed/mapped." part, right?

     foo(externalName internalName: Type) { ... }
Doesn't this strike you as odd at all? The part that looks like the label is not the label, it's the name of the variable. And then there's this extra bit in front that doesn't look like the label, doesn't really look like anything TBH, that will turn into the label. If present.

I kind of see how they got there, but boy is it inconsistent.


I don’t know it seems fine to me tbh but maybe it’s a matter of taste.

It seems pretty simple to me: everything before the colon is defining the argument name. If you have one label, it means the argument label is the same as the name of the parameter used inside the function. If you have two labels, then the first one is the argument label and the second one is the variable name.

You could also think of it that you’re always specifying the argument label and the variable name, and in the case where they’re identical you have a bit of shorthand to save some typing.

Maybe it seems weird to some people if it’s not familiar, but I got used to it pretty quickly after starting to use Swift, and it felt like a natural extension of the type system and optional handling in terms of the language giving me tools to cross-check things and bake sanity checks into the source code itself.


> it’s a matter of taste.

Probably. ¯\_(ツ)_/¯

> everything before the colon is defining the argument name.

So the space binds more strongly than the colon. That seems..."unusual".


I have never used a language which allow this, is-it really useful? I don't think I have ever thought if only the external name was different than the internal name or used an 'alias' variable inside the function to 'hide' the external name..




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: