Hi, I have a requirement to make use of two typeclasses `Async<F>, ApplicativeError<S, Nel&...
g
Hi, I have a requirement to make use of two typeclasses
Async<F>, ApplicativeError<S, Nel<E>>
. I achieved it as below:
Copy code
interface RepoTC<F> : Async<F> {
    fun User.isUserCityValid(): Kind<F, Boolean>

    fun <S> ApplicativeError<S, Nel<ValidationError>>.userCityShouldBeValid(user: User) = fx.async {
        val cityValid = user.isUserCityValid().bind()
        if (cityValid) this@userCityShouldBeValid.just(cityValid)
        else raiseError(UserCityInvalid(user.city).nel())
    }
}
But it just doesn’t feel right, as: - It can’t be extended if I need other typeclasses. - I see
ApplicativeError
in
Async
hierarchy. So do I need both the TCs? I tried this, but am getting compiler errors due to clashes.
Copy code
interface RepoTC2<F, S> : Async<F>, ApplicativeError<S, Nel<ValidationError>> {
    fun User.isUserCityValid(): Kind<F, Boolean>

    fun userCityShouldBeValid(user: User) = fx.async {
        val cityValid = user.isUserCityValid().bind()
        if (cityValid) just(cityValid)
        else raiseError(UserCityInvalid(user.city).nel())
    }
}
Can I achieve it just by using Async itself?
r
in meta you’d get them auto injected but in this case currently the alternative is to use them as abstract functions in the interface or properties
since they subtype each other but the types they target are different F and S
g
Thanks @raulraja, can u pls elaborate “alternative is to use them as abstract functions in the interface or properties”
r
don;t extend them but instead use them as properties or inject them via constructor as dependencies
g
oh ok
got it
r
then you may use
TC.run { }
if you want a block with the syntax of any of them
g
Sure, I shall try it out and get back
Thanks @pakoito! Will try it out