Whereas the post's example compares:
path.addLineToPoint(CGPoint(x: 100, y: 0))
-- to --
path.addLineTo(CGPoint(x: 100, y: 0))
I've been doing it as so: path.addLine(toPoint: CGPoint(x: 100, y: 0))
...requiring the "toPoint", which can swapped out true method overloading style: path.addLine(toArc:...)
In your internal method implementation, Swift allows you to replace the external method name with an internal one, so that it's still nice to work with: func addLine(toPoint point: CGPoint) {
...
}
I personally think it's a lot more readable. Otherwise the first argument has to have a really descriptive class name (recommended for sure, but often not the case). path.addLine(toPoint: CGPoint(x: 100, y: 0))
See the "toPoint: CGPoint". It's useless repetition. The new form eliminates it: path.addLineTo(CGPoint(x: 100, y: 0))
It's clear you're adding a line to a point, because it says "Point" right there. Even if you have a variable for this point already, it works great: path.addLineTo(point)
Or maybe this is a more specifically named point? path.addLineTo(centerPoint)
It's clear from all that you're adding a line to a point. Swift has an emphasis on concise syntax, and removing the repetition, IMO, is a nice win in readability.It'd also be interesting to see the impact on readability when you have longer variable names. `point` definitely makes it more concise here. But if you had several points within the same scope, readability may suffer given type information is typically at the end of a variable name?
path.addLineTo(childViewTopLeftPoint)
path.addCurveTo(childViewTopRightPoint, withControlPoint1: arbitrary1Point, andControlPoint2: arbitrary2Point)
path.addLine(toPoint: childViewTopLeftPoint)
path.addCurve(toPoint: childViewTopRightPoint, withControlPoint1: arbitrary1Point, andControlPoint2: arbitrary2Point)
I feel the second version here allows me to bypass obtaining the type information "..Point" from the variable name when reading.Interestingly, I wonder how this type of method would be converted:
addArcWithCenter(center: CGPoint, radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat, clockwise: Bool)
You'd perhaps think of addArcWith(center: CGPoint), but then you'd need to have 'center' in the variable name to convey meaning. Keeping addArcWithCenter maintains obj-c status quo. addArc(withCenter: CGPoint) is more Swifty, but you may have repetition if your variable is named centerPoint, or similar. I have a feeling it would be kept as is.So the class UIBezierPath and it's method addLineToPoint: haven't changed, and are still written in Obj-C. The difference is in how they're called from Swift code.
Presumably it'll operate the same way in reverse, and this isn't specific to Apple frameworks/classes.
Something like this:
class BezierPath: NSObject {
func add(LineTo lineTo: CGPoint) {}
func add(ArcWithCenter center: CGPoint, radius: CGFloat,
startAngle: CGFloat, endAngle: CGFloat,
clockwise: Bool) {}
func add(CurveTo endPoint: CGPoint, controlPoint1 point1: CGPoint,
controlPoint2 point2: CGPoint) {}
func add(QuadCurveTo endPoint: CGPoint, controlPoint: CGPoint) {}
}
class main {
func run() {
let path = BezierPath()
let point1 = CGPoint()
let point2 = CGPoint()
path.add(LineTo: CGPoint(x: 100, y: 0))
path.add(ArcWithCenter: CGPointZero, radius: 20.0,
startAngle: 0.0, endAngle: CGFloat(M_PI) * 2.0,
clockwise: true)
path.add(CurveTo: CGPoint(x: 100, y: 0), controlPoint1: point1,
controlPoint2: point2)
path.add(QuadCurveTo: point2, controlPoint: point1)
}
}
The difference is mostly that it looks even more concise when using code completion. You would see: path.add(LineTo: CGPoint)
Instead of: path.addLineTo(point: CGPoint)Objective-C
[aPath addLineToPoint:CGPointMake(200.0, 40.0)];
proposed aPath.(addLineToPoint:CGPoint(200.0, 40.0));
the more complicated example would be:Objective-C
[object selector1:item1 selector2:item2];
to object.(selector1:item1, selector2:item2);
although I'm still not sure about the love of commas aPath.addLineToPoint(CGPoint(200.0, 40.0))
I don't see how your proposal would fit with Swift's syntax.I wouldn't want to see, for example:
collectionView.(dequeueReusableCellWithReuseIdentifier:identifier, forIndexPath: indexPath)
because that obscures the primary purpose of the method, which is to dequeue a cell. To me, this is preferable: collectionView.dequeueReusableCell(reuseIdentifier: "ImageCell", îndexPath: indexPath)
The long first component of the Objective C selector ("dequeueReusableCellWithReuseIdentifier") is just an artifact of ObjC's lack of distinction between method names and parameter names, which forces the API creator to use words like "With" to accomplish what Swift can do natively.What are some examples of methods you think make more sense with your syntax?