Reputation: 6278
I'm doing a lot of positioning and animation stuff in literals, and they're taking up a lot of space and becoming unreadable because of the verbosity.
What I'd like to do is turn this
var xy = CGPoint(x: 100, y: 100)
Into this:
var xy = •(100, 100)
Similarly, I'd like to go from:
CGVector(dx: 200, dy: 200)
to something like this:
`(200, 200)
But I don't know how a macro-like shorthand or something like this would be done, or even if it could be done. Any pointers (puntendered) would be better than where I'm at.
I get that this is probably, for most people, less readable.
Due to the context I always know what these are. I don't need the parameter labels or function name to understand what I'm doing. And this is private animation and motion testing, there's no need for anyone else to ever understand what's going on.
Upvotes: 0
Views: 553
Reputation: 535306
I too like to get rid of the parameter labels, but I don't advise reducing things to any briefer shorthand. I paste this code into the start of all my app projects:
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
extension CGSize {
init(_ width:CGFloat, _ height:CGFloat) {
self.init(width:width, height:height)
}
}
extension CGPoint {
init(_ x:CGFloat, _ y:CGFloat) {
self.init(x:x, y:y)
}
}
extension CGVector {
init (_ dx:CGFloat, _ dy:CGFloat) {
self.init(dx:dx, dy:dy)
}
}
That allows me to say things like CGPoint(1,4)
, and that's good enough, in my opinion.
Upvotes: 1
Reputation: 8411
You can't use special characters as the compiler would not be able to differentiate between function names and operators like '*'.
Using typealias
using a typealias would allow you to shorten the function name but not omit the parameter names like you outlined in your question.
typealias p = CGPoint
//example
let p1 = p(x: 60, y: 60)
Using anonymous parameters
func p(_ x: CGFloat, _ y: CGFloat) -> CGPoint {
return CGPoint(x: x,y: y)
}
//example
let p1 = p(60, 60) //no parameter names
Upvotes: 1
Reputation: 19750
For this type of thing you would likely use a typealias
but I don't think you will be able to create types with names as operators or symbols.
See below, I created a typeAlias
of CGP for CGPoint, you could simply just alias it to Point if you like.
Here is some information from the docs
A type alias declaration introduces a named alias of an existing type into your program. Type alias declarations are declared using the typealias keyword and have the following form:
typealias name = existing type
After a type alias is declared, the aliased name can be used instead of the existing type everywhere in your program. The existing type can be a named type or a compound type. Type aliases do not create new types; they simply allow a name to refer to an existing type.
EDIT
Two simple method of excluding the parameter names:
func cp(_ x: Int, _ y: Int) -> CGPoint {
return CGPoint(x: x, y: y)
}
let point = cp(0,2)
extension CGPoint {
init(_ x: Int, _ y: Int) {
self.init(x: x, y: y)
}
}
typealias CGP = CGPoint
let point2 = CGP(1,1)
The first method just creates a helper function which returns a CGPoint using it's designed intialiser. The second option is the better way of doing it, you extend CGPoint and add an intializer that marks the params as non-named params (by using the _ instead of the external name)
Upvotes: 1
Reputation: 6195
extension CGPoint {
init(_ x: CGFloat, _ y: CGFloat) {
self.init(x: x, y: y)
}
}
extension CGVector {
init(_ dx: CGFloat, _ dy: CGFloat) {
self.init(dx: dx, dy: dy)
}
}
typealias P = CGPoint
typealias V = CGVector
let p = P(10, 10)
let v = V(10, 10)
But no idea about the • and ` part - I replaced them with P and V :)
Upvotes: 2