“Type methods are similar to type properties. Use a type method when the action is related to the type, but not something that a specific instance of the type should perform.
”
Can some please explain this statement with a simple example?
It comes down to the difference between a type and an instance of that type.
I always explain it as the difference between a biscuit cutter and a biscuit.
The biscuit cutter has the pattern for the biscuit.
A type has the "design" of the objects (instances) it is going to create.
Type properties describe how the "biscuit cutter" looks (its shape, material, etc) - type methods are the things that you can do to the "biscuit cutter" (cut biscuit, eject biscuit, clean, etc)
Instance properties describe the "biscuits" (chocolate, wholegrain, soft, hard, etc) - instance methods describe what you can do to a biscuit (eat, crumble, dunk, etc)
Does that help?
1 Like
always start with a type method, then, if you realize the method needs access to the instance, self, “upgrade” it to an instance method. (similar strategy works for func and mutating func)
sveinhal
(Svein Halvor Halvorsen)
5
… and value types vs reference types.