I see that constants in the standard library and other Apple libraries are defined like this:
public var M_PI: Double { get }
rather than eg:
public let M_PI: Double = 3.1415...
and like this:
public struct UInt8 : ... {
public static var max: UInt8 { get }
}
rather than eg:
public struct UInt8 : ... {
public static let max: UInt8 = 255
}
Why is that?
How is a read-only computed property that returns a constant different from a simple let?
That is, how is constantA different from constantB:
var constantA: Int { return 1234 }
let constantB = 1234
?
(I'd like to know about any and all differences, even subtle ones, and also global vs struct, class, function scope, debug vs release builds, etc.)
The promise of 'let' is that the value will not change during its lifetime. For globals, that's the entire life of the program; for properties, that's the life of the containing object. A get-only 'var' is allowed to return a different value every time.
In practice it would probably be fine for most constants coming from C to be imported using 'let', but the compiler currently doesn't differentiate.