Closed jamiehodge closed 2 years ago
Just a side note but uuid
is not a pure function so the example IMHO illustrates a wrong usecase of a lazy withFallback
version.
@mlegenhausen What's wrong with an impure function here and how would you instead structure it? I would use it for exactly the same use case as Jamie.
Normally I would expect impure code to run in something IO
or Task
related. Until now running Decoder.decode(someType)
resulted always in the same result (pure). Using uuid
in this context changes the behavior.
My solution for this problem would be to extract the impurity. For example the uuid package allows to provide all parameters that result in the uuid to be passed via an options parameter.
const mkCodec = (uuidOptions) => t.interface({
id: withFallback(UUID, uuid(uuidOptions))
...
})
Maybe the discussion goes beyond the io-ts
field. When you use io-ts
without fp-ts
this usage with a side effectful function seems reasonable.
To support, for example,
withFallback(UUID, () => uuid())
.