IanKeen / iankeen.github.io

Source for my blog
http://iankeen.tech
Other
0 stars 0 forks source link

Type safe temporary models #7

Open IanKeen opened 6 years ago

IanKeen commented 6 years ago

Comments for: http://iankeen.tech/2018/06/05/type-safe-temporary-models/

zapko commented 6 years ago

Hey Ian, That's an interesting idea, thanks for sharing!

Am I understanding correctly that this approach doesn't distinguish between optional values being set to nil and not being set at all?

IanKeen commented 6 years ago

hey @zapko , thats right! When you set an optional property to nil it removes it from the underlying dictionary.

When you eventually come to extract the data if the target is optional this shouldn't matter since the generic type should define what data you want at the end.

So if a property should be non-optional and you don't give it a value in the partial data structure it will fail when you attempt to produce the 'real' model at the end.

zapko commented 6 years ago

Yeah, so if I want to calculate next step of a flow based on what data has been retrieved so far - it's not the approach I should follow. Or is there a way, what do you think?

IanKeen commented 6 years ago

well thats more 'business logic' - you can use this to help determine whats next, but its not something that this structure should handle on its own, for example your view controller/view model/etc could do something like:

func nextStep() -> Step {
   if (try? partial.value(for: \.firstName)) == nil { return .firstNameStep }
   if (try? partial.value(for: \.lastName)) == nil { return .lastNameStep }
   return .complete
}

Probably a good candidate for adding a contains(_:) to Partial<T> 👍