First question here on CodeReview. I was directed here from a question I asked on SO: Swift protocol with lazy property - Cannot use mutating getter on immutable value: '0ドル' is immutable.
Goal: Create a protocol that allows lazy computation of a property for structs that conform to the protocol. The computation is intensive and should only be executed once, hence the lazy requirement.
So, after lots of reading (eg Swift Struct with Lazy, private property conforming to Protocol, where I based this code on) and trial and error, I came up with something that works:
import Foundation
protocol Foo {
var footype: Double { mutating get }
func calculateFoo() -> Double
}
struct Bar: Foo {
private lazy var _footype: Double = {
let value = calculateFoo()
return value
}()
var footype: Double {
mutating get {
return _footype
}
}
func calculateFoo() -> Double {
print("calc")
return 3.453
}
}
Testing this in a Playground:
var bar = Bar()
print(bar.footype)
print(bar.footype)
And the output is:
calc
3.453
3.453
So it is working. But it feels like a hack using _footype
Also in the comments, it was suggested that
However, I would not put calculateFoo in the protocol because it sounds like an implementation detail.
Appreciate any comments and/or improvements.
-
1\$\begingroup\$ Welcome to Code Review! \$\endgroup\$Martin R– Martin R2019年10月13日 17:19:01 +00:00Commented Oct 13, 2019 at 17:19
1 Answer 1
You don't need the additional _footype
variable and a computer "wrapper" property. The protocol requirement can be satisfied with a lazy stored property:
struct Bar: Foo {
lazy var footype = calculateFoo()
func calculateFoo() -> Double {
print("calc")
return 3.453
}
}
Now footype
is read-only for instances of type Foo
var foo: Foo = Bar()
print(foo.footype)
foo.footype = 12.3 // Error: Cannot assign to property: 'footype' is a get-only property
but as a property of Bar
it is read-write:
var bar = Bar()
print(bar.footype)
bar.footype = 12.3 // OK
If assigning to the footype
property should be inhibited then you can mark it with a private(set)
access modifier:
struct Bar: Foo {
private(set) lazy var footype = calculateFoo()
func calculateFoo() -> Double {
print("calc")
return 3.453
}
}
var bar = Bar()
print(bar.footype)
bar.footype = 12.3 // Cannot assign to property: 'footype' setter is inaccessible
With respect to
However, I would not put calculateFoo in the protocol because it sounds like an implementation detail.
Yes, in your current code it is an implementation detail of the Bar
class. The only use would be that a caller can "enforce" the evaluation:
var foo: Foo = Bar()
foo.calculateFoo()
The situation would be different if there were a way to provide a default implementation in an extension method:
protocol Foo {
var footype: Double { mutating get }
func calculateFoo() -> Double
}
extension Foo {
var footype: Double {
// Call calculateFoo() on first call only ...
}
}
so that conforming to the protocol is sufficient, i.e. Bar
only has to implement the calculateFoo()
method, but not the footype
property.
But I am not aware of a way to do this lazily so that the function is called only once. The reason is that extensions cannot add stored properties to a type.
For subclasses of NSObject
you can come around this problem with "associated objects," but not for structs.
-
\$\begingroup\$ Yes,
footype
is calculated only, never set. So, just to be sure, to be able to use thelazy
property, I need to usemutating
invar footype: Double { mutating get }
in the protocol? \$\endgroup\$koen– koen2019年10月13日 17:09:41 +00:00Commented Oct 13, 2019 at 17:09 -
1\$\begingroup\$ @Koen: Yes (unless you make Foo a "class-only" protocol:
protocol Foo: class {...}
andBar
a class instead of a struct). \$\endgroup\$Martin R– Martin R2019年10月13日 17:15:56 +00:00Commented Oct 13, 2019 at 17:15 -
\$\begingroup\$ yes, it has to be a struct, due to other parts of my code. \$\endgroup\$koen– koen2019年10月13日 17:19:58 +00:00Commented Oct 13, 2019 at 17:19
-
\$\begingroup\$ And the implementation of
calculateFoo()
is different for the various structs that conform toFoo
, so they must all implement it for their specific case. \$\endgroup\$koen– koen2019年10月13日 17:39:47 +00:00Commented Oct 13, 2019 at 17:39 -
\$\begingroup\$ There is no need to wrap
calculateFoo()
call into closure. You can writelazy var footype: Double = calculateFoo()
, and it will work as expected,calculateFoo
will be called just once, and only when the variable is accessed for the first time. \$\endgroup\$Egor Komarov– Egor Komarov2019年10月31日 00:48:04 +00:00Commented Oct 31, 2019 at 0:48