Skip to content

Instantly share code, notes, and snippets.

@ole
Created October 29, 2019 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ole/ae0c791030ed44aade9a823edf0a6aeb to your computer and use it in GitHub Desktop.
Save ole/ae0c791030ed44aade9a823edf0a6aeb to your computer and use it in GitHub Desktop.
Using × as a custom operator for creating "x-by-y" values such as CGPoint or CGSize.
precedencegroup XByYPrecedence {
associativity: left
higherThan: MultiplicationPrecedence
}
infix operator ×: XByYPrecedence // U+00D7 MULTIPLICATION SIGN
// ------
import CoreGraphics
func × (x: CGFloat, y: CGFloat) -> CGPoint {
CGPoint(x: x, y: y)
}
func × (width: CGFloat, height: CGFloat) -> CGSize {
CGSize(width: width, height: height)
}
let point: CGPoint = 100×80
let size: CGSize = 800×600
// ------
import UIKit
let image = UIGraphicsImageRenderer(size: 320×240).image { context in
UIColor.yellow.setFill()
UIRectFill(CGRect(origin: 20×20, size: 280×200))
}
// ------
import SceneKit
func × (xy: CGPoint, z: CGFloat) -> SCNVector3 {
SCNVector3(xy.x, xy.y, z)
}
let point3D: SCNVector3 = 10×20×30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment