Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temperature.celsiusValue should be a computed property #157

Open
stephencelis opened this issue Aug 24, 2015 · 1 comment
Open

Temperature.celsiusValue should be a computed property #157

stephencelis opened this issue Aug 24, 2015 · 1 comment

Comments

@stephencelis
Copy link

The problem with a lazy var is you can overwrite it:

lazy var celsiusValue: Int = {
return Int(round(Float(self.fahrenheitValue - 32) * 5.0 / 9.0))
}()

let temp = Temperature(fahrenheitValue: 20)
temp.celsiusValue = 20

The math isn't particularly expensive, so using a computed variable is probably good enough. If we really want to lazily load the celsius value, we can do so with a private lazy var, which the computed variable calls/returns.

@jakecraige
Copy link
Contributor

Interesting find. Computed variable makes sense to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants