3 ms·
What was the design decision that required function declaration to be : func hello(name: String) -> String { } rather than, func hello(name: String) : String
by crudbug 11y ago
What was the design decision that required function declaration to be :
func hello(name: String) -> String { }
rather than,
func hello(name: String) : String { }
- zkirill 11y agoYou might find this handy: http://fuckingswiftblocksyntax.com/ http://fuckingswiftblocksyntax.com/
- crudbug 11y agoThanks for the link. I think, the function type : (Int, Int) -> Int may have been the main reason, but having (Int, Int) : Int func nameMe(name: String, calName: ((Int, Int) : Int)) would still have worked, and made the syntax more aesthetic.
- throwaway999888 11y agoIt seems pretty consistent with functions in a set-notation sense, with functions that map a tuple of stuff to something else. The mapping being indicated by an arrow instead of a colon.
- crudbug 11y agoI was thinking more about Types here var name : String func nameMe(name: String) : String
- throwaway999888 11y agoI was half-thinking the same, but types and sets feel interchangeable on an intuitive level in such cases. Having both `: String` and `: String` seems to suggest that they are the same type, which they are not. `nameMe(_)` is a function type, while `name` is a "plain value", so to speak. I think that using arrows more clearly indicates that it's a mapping from something to something else -- in the case of Swift, I guess a mapping from a tuple to something else. And think about anonymous functions, in general since I haven't looked into Swift anonymous functions. If you indicate a function like this: `func nameMe(name: String) : String` How are you supposed to indicate an anonymous function? Like this: `(name: String) : String` Then it seems to say that name is a type of String, which in turn is a type of String. I guess you could add something like a lambda to distinguish it: `(λ name => name): String` But now you've still effectively said that both `name` (a value of type string) is the same type as the anonymous function (a function with one argument). That's clearly wrong. Using arrows for functions seems to be better when you start to want to talk about types and values by themselves, like: `1: Int`, `"hello": String`, `(λx => x) : a -> a`
- crudbug 11y agoPoint well received. BTW Fat arrow looks more appealing than simple arrow. All the other pieces look very well designed.
- throwaway999888 11y agoThe single-line arrow has both precedent in set theory, type theory, and mathematical logic.
- crudbug 11y agoThe functional mapping also makes sense. Thanks for the pointer.
- jaxondu 11y agoRust is using similar declaration: fn hello(name: str) -> str { }