One thing to keep in mind as a (non-satisfying) reason is how Kotlin treats everything as an expression, while Swift has semantic differences between Properties, Functions, Methods, and Closures differently. That, along with a more complex memory-management system I believe means some of the syntactic sugar you get for free in Kotlin is significantly complex to implement in Swift.
Swift is also designed for clarity and safety, while Kotlin sacrifices some amount of those for a more flexible and script-like syntax. So although I would love to do things in Swift like this:
func add(_ left: Int, _ right: Int) = left + right
func add1(to value: Int) = add(value, 1)
func addIf0(value: Int) = switch isAllowed {
case 0: add1(to: value)
default: value
}
there are reasonable arguments that it's much less clear what's happening, and therefore less safe.
Anyway, my guess is that type of syntax isn't coming to Swift anytime soon, just like static properties/methods, enum associated values, and turnery statements probably aren't coming to Kotlin.
As noted by @GetSwifty, there are substantial differences between Swift and Kotlin that make harder for Swift to push syntactic sugar too far.
Fortunately, the proposal linked by @jawbroken already makes easier on the eyes single expression functions, improving consistency between named functions and lambdas.
That being said, I disagree with the improvements in readability and understandability achieved by replacing {} with =: I think it's a lot clearer to wrap the body of a function in curly braces than to "assign" a single expression to the function. But even if it wasn't, I don't see profound differences once you can skip the return, which is to me the real offender. For example, I don't see how
public func hash(into hasher: inout Hasher) {
hasher.combine(SGLMath.hash(x.hashValue, y.hashValue))
}
is hurting your eyes more than the = counterpart. Something that could be added is the type inference on computed properties, for example:
public var debugDescription {
String(describing: type(of: self)) + "(\(x), \(y))"
}
would be better, the : String is redundant (from a readability standpoint), but something similar is achieved with .init, like:
public var debugDescription: String {
.init(describing: type(of: self)) + "(\(x), \(y))"
}
so a nice balance was achieved by the aforementioned proposal.
It's not the fault of the language if whatever auto-formatting tools you are using are mangling your code in ways you don't prefer, and I really don't think that new redundant syntax should be added just to stop them from doing that.
I'm not very keen on this - purely from the point of reading code, it makes it somewhat unclear whether you are assigning a function/expression or the result of executing the function to the variable. Obviously you can tell from the type and, sometimes, the body, which it is - but I'm not sure the extra effort required and potential for confusing people new to the language is worth it.
I find...
public var debugDescription: String = String(describing: type(of: self)) + "(\(x), \(y))"
...to be the most potentially confusing. Surrounding the code in brackets makes it totally unambiguous that this will be evaluated every time you call debugDescription, rather than...when the object is instantiated? When it's first called? Every time?
Just to be clear, Kotlin doesn't replace brackets, it allows you to use either
Given Swift's opinionated standardization, I think I agree allowing = in a function definition is confusing and easily abused. However I also find it quite elegant in Kotlin to do things like
fun makeFullName() = "$firstName $lastName"
// or
val fullName: String get() = "$firstName $lastName"
I especially like it when using when, e.g. for an enum:
Yes you can look at it as muddling the definition of a function/method, and I used to agree. What changed my mind for things like the above is taking it to mean "makeFullName() = ThisExpression".
This is why Kotlin's "everything is an expression" construct IMO means it makes sense in Kotlin, and may not make sense in Swift. I don't think there's an existing analogy you can intuit for what func thing() = would mean.
Still, personally I would still like a way for single expressions to be written more concisely in Swift. Maybe something like
func fullName() -> String in "\(firstName) \(lastName)"
also, conciseness isn’t everything, I like concise, expressive code, but not a the cost of potential ambiguity and less approachability.
Still, I like the idea that things like if and switch should be expressions (not everything, though, for example guard is great as it is, and it’s one of the things I miss the most in Kotlin): if they were, you could drop the ‘return’, which seem to me the real offender to me, that is, a lot of additional characters that are redundant and add noise.
but my guess is an inferred Type for a function result would see a lot of pushback.
A couple brackets isn't a huge deal, but that's also true of parens for if/switch statements among other things. It wouldn't be earth shattering, but I would prefer the ergonomics in some cases.
It's indeed pretty plain and natural extracting the return at the end
return if(cond) a else b
return when(cond) {
a -> x
b -> y
}
Regarding guard I find the concept really fascinating, but the implementation quite poor..
having to explicitly type every single time else { ... return } is quite verbose and annoying.. I'd have expected a default nil for Optionals and plain return with Void (maybe passing a string in a autoclosure)
Once you understand what's happening, I agree it's verbose. However, if you're new to programming or new to swift, I don't think it would be possible to intuit what's happening. If I came to Swift and saw
IMO it's much clearer and easy to reason about what might be happening, and makes it clear that author's intention is to return nil if thing isn't ready.