Why I'm getting: error: value of type 'Double' has no member 'scale'

var x = 1.0
let s = 0.5
x.scale(by: s)
print("x = \(x)")

swiftc scale.swift
scale.swift:3:3: error: value of type 'Double' has no member 'scale'

But .scale(by:) is documented here: https://developer.apple.com/documentation/swift/double/3299111-scale

:astonished: :astonished:

There's " Framework SwiftUI" in the column on the right. What happens if you import SwiftUI?

1 Like

As @cukr mentioned, the scale(by:) method is an extension on Double which is only available in SwiftUI.

1 Like

Thanks! I need to remind myself things can be added by anything in Swift and look for its implementation source.

I was just trying to verify what .scale(by:) is just multiply, and it appears to be the case. Wonder what's the rationale for adding this, instead of simply just do x *= s. Maybe just to satisfy some mathematician's notion of what multiply mean?

Maybe just to satisfy some mathematician's notion of what multiply mean?

Just the opposite! :joy:

It comes from the VectorArithmetic protocol in SwiftUI; if it were just a concrete method on Double, they would use *=, but there are some drawbacks to following that approach with more general types (mostly around limitations of the current type inference system).

3 Likes

Make perfect sense now. Scale is correct for vector math.

Just realize, I shouldn't be using Double.scale(by:) in my code, because I'm not doing VectorArithmetic.