by jmull · 6y ago · 0 comments
The rest look like they aren't worth complicating the language over. Consider:
map(\.name)
Isn't really more concise than map { $0.name }
There's one more character and the conventions for whitespace are different. Both require that you know special Swift syntax sugar features, but at least the second one is more generally useful. All we get here is a virtually identical way to do the same thing but with alternate arbitrarily different syntax.And what's better... this?
struct Dice {
var lowerBound: Int
var upperBound: Int
func callAsFunction() -> Int {
(lowerBound...upperBound).randomElement()!
}
}
let d6 = Dice(lowerBound: 1, upperBound: 6)
let roll1 = d6()
or this? struct Dice {
var lowerBound: Int
var upperBound: Int
func roll() -> Int {
(lowerBound...upperBound).randomElement()!
}
}
let d6 = Dice(lowerBound: 1, upperBound: 6)
let roll1 = d6.roll()
callAsFunction() makes it a little harder to relate the calling code to the implementing code -- you've got to internalize the new syntax-sugar rule -- and the calling code gets a little less clear in some cases.Default arguments to subscript functions is probably OK. It's better if all kinds of function have the same capabilities.
It's for Swift for TensorFlow's integration with Python, in which a bridged Python object can be called as a function. Functions are, of course, objects in Python, so that makes sense, but it would be a pain to have to call fn.call(bar) instead of fn(bar) when fn is a reference to a python object that is a function in its runtime.
It's also used in TensorFlow models, in which callAsFunction() is usually implemented as passing the model's input through all the defined layers in it.
This makes it easy to treat models as if they were functions, which they mathematically are, unlike Dice.
Example:
struct MLP: Layer {
var layer1 = Dense<Float>(inputSize: 2, outputSize: 10, activation: relu)
var layer2 = Dense<Float>(inputSize: 10, outputSize: 1, activation: relu)
@differentiable
func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
let h0 = layer1(input).withDerivative { print("∂L/∂layer1 =", $0) }
return layer2(h0)
}
}
(from: https://www.tensorflow.org/swift/tutorials/custom_differenti... )It's a bad example enabled by a bad language feature. Just you wait until answers on stack overflow, sample code, and open source libs are littered with these "bad examples".
Meanwhile, this example is terrible. There is no real need for callAsFunction in your example when you could have added a simple named function on the object to do so. Better yet, you could define a protocol and make sure your layer conforms to said protocol.
Any feature, no matter how good, can be misused. Pointing that out about a specific feature isn't interesting, because it is true of every feature. And the absence of nearly any feature can be worked around, as ultimately all these features compile down to machine code, which doesn't have most features of a high-level language, so the existence of a workaround is also not a very compelling argument.
“Callability” not being restricted to functions (closures or not) can often be convenient, especially when that use is colloquial (eg closure where symbols and collections are callable), and obviate unnecessary boilerplate.
Functions are objects in many langages. Python’s specific feature is that there are callable a beyond functions. A class is a callable and a user-defined object can be one despite neither being functions.
Clojure also has that concept.
Keypaths are used in other places and useful. But they aren’t used very often, and I felt I was forgetting their syntax when I first learned Swift.
By making them useful in two areas with largely the same semantics, it helps you understand and use them in practice more.
In the end it’s all fairly subjective, but it doesn’t bother me as a relative newcomer.
This was the exact motivation. It was part of a refactoring to clean up some implementation details and resulted in a net deletion of lines of code.
func die(lowerBound: Int, upperBound: Int) -> () -> Int {
{ (lowerBound...upperBound).randomElement()! }
}
let d6 = die(lowerBound: 1, upperBound: 6)
let roll1 = d6()
so the syntax already exists.But does it really work out? Surely you can't put these new structs into the same arrays as similar lambdas, can you?
The proposal doesn't seem to have one.
https://github.com/apple/swift-evolution/blob/master/proposa...
(The last example in your link shows using a keypath to specify a sort criteria. That's a nice use of keypaths, but isn't helped by this new feature.)
Never underestimate peoples (often irrational) hatred for sigils, or even things that remind them of sigils.
Language designers seem to underestimate the cognitive overhead of syntax duplication.
StateStream.map { $0.subState }.compactMap(\.subObject).flatMap { $0.objectProperty }
Being correct is super frustrating.