4 ms·
Could someone tell me the meaning of _ in this Swift code: func greet(_ name: String,_ day: String) -> String { return "Hello \(name), today is \(d
by kodfodrasz 9y ago
Could someone tell me the meaning of _ in this Swift code:
func greet(_ name: String,_ day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
In me prior experience usually _ denoted an unused parameter/variable.
Also: I find the string interpolation syntax ugly... The syntax choices in Swift are curious, but not the most aesthetical/practical in my opinion.
- deleted 9y ago[deleted]
- myhf 9y ago_ in function parameters indicates that a parameter can be passed by position, without its name. Without _ you would have to call greet(name: "Bob", day:"Thursday") but with _ you can call greet("Bob", "Thursday")
- Larrikin 9y agohttps://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html https://developer.apple.com/library/content/documentation/Sw... Swift forces you to use named parameters if you don't do that.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- Watabou 9y agoSwift has both external and internal parameter names for functions. This is to make API design more readable, easier to parse through. For example, you can have a function like so: func greet(with greeting: String, to personName: String) { print("\(greeting), \(personName)!") } In the above code, "with" and "to" are external names, and are only available when calling the function but are not available inside the function itself. So you would call this function like so: greet(with: "Hello", to: "Bob") Now if you want to exclude the external name to call the functions, you use the "_" syntax. So you're right that it denotes an unused parameter. In this case, it's an unused external parameter name.
- derefr 9y agoIs there a parse-time reason that they required the "_" syntax, rather than simply: func greet(greeting: String, personName: String) { print("\(greeting), \(personName)!") }
- thramp 9y agoIt's a side effect of labeled arguments. The caller would use the function greet as `greet(greeting: "Hello", personName: "Alice")`.
- masklinn 9y agoThe reason is that named parameters is the default, so in your case greeting and personName are both "internal" and "external" names. "_ greeting" sets the external name to _ (aka positional) and the internal name to "greeting" instead.
- saghm 9y agoHaving never used Swift, it feels like the syntax is being optimized for the less-common case; are named parameters considered the "default" choice in Swift, and is using different external names so common that it's worth cluttering the syntax for functions without external parameters in order to make the their own syntax easier?
- jurip 9y agoYou can argue if it's useful or not on its own, but it makes sense when you consider that Swift is compatible with Objective-C. In Objective-C, that function would be a method like -(void) greetWith:(NSString *)greeting to:(NSString *)personName and you'd call it like [self greetWith:@"Hello" to:@"Bob"] Swift's external/internal parameters are built to allow the same style as was used with Objective-C and easy bridging. Swift's APIs tend to be terser than Objective-C ones, though.
- ridiculous_fish 9y agoNamed parameters are very much the default in Swift. Positional parameters are rare exceptions. The internal/external name divide often works out beautifully. For example, let's equip Double with a multiply-add. An idiomatic Swift signature: extension Double { func multiply(by multiplier:Double, adding summand:Double) -> Double { return self * multiplier + summand } } print(3.multiply(by:4, adding:5)) Erase punctuation and you have: func multiply by multiplier adding summand print 3 multiply by 4 adding 5 The caller sees an action (multiply by, adding), the callee sees nouns (multiplier, summand). It's fantastically readable.
- gbanfalvi 9y agoYou can name parameters externally and externally explicitly. func sayHi(to personName: String) { print("Hi \(personName)") } sayHi(to: "Bob") // Hi Bob Similarly, using just one label in the function declaration means that they have the same name externally as well as internally. In this case the example is saying that they don't want any external labels. edit: damnit, beaten
- Pxtl 9y agoI dunno, I think Swift's string interpolation is clever. Using the special-char backslash for opening string interpolation is a neat idea. Also keeps the number of special chars in in strings to a minimum.