Youssef Shoaib [MOD]
09/05/2020, 7:28 PMjust
be implemented for any Functor by using an instance of that functor and calling map on it with a function that disregards it's results and just returns whatever value was passed in. So something like this
inline fun <reified F,A,B> Kind<F,A>.Companion.just(value: B): Kind<F,B> {
return getSomeStaticValueForFunctor<F>().map { B }
}
So instead of having Applicative, couldn't there be just a FunctorWithStaticInstance
that then Apply
inherits from (and by extension Monad
also inherits from). Additonally, the docs mention that some classes can implement Apply
but not Applicative
, but isn't it guaranteed that every typeclass in general will have an instance? so then with a pretty simple implementation you could probably produce a Functor<Unit>
that will be a val
on FunctorWithStaticInstance
. Or maybe even every functor could just have a static instance by default. Maybe then you could even have just
on Functor
itself. I'm sure that there's definitely something wrong with my reasoning, but I honestly cannot figure it out.raulraja
09/06/2020, 8:36 AMjust
for example all the mapN and product variations. You could reencode as you mention but that would form in fact an Apply or Applicative since just
is required for types to be constructed and that is in essence what apply can do. The static factory would replace that but it does not change the semantics of what you are doing. There may be types that are just functor and have no apply though rare and for the same reasoning we could have invented a type class called Just
with it’s laws. Whether a type provides a value statically or a new instance through a constructor is rather a detail that IMO FP largely ignores, it’s still the “just” constructor in disguise.raulraja
09/06/2020, 8:39 AMraulraja
09/06/2020, 9:44 AMYoussef Shoaib [MOD]
09/06/2020, 9:51 AMYoussef Shoaib [MOD]
09/06/2020, 9:53 AMraulraja
09/06/2020, 9:56 AMinterface Raise<E> {
suspend fun raise(e: E): Nothing
}
raulraja
09/06/2020, 9:58 AMF
in the type class since suspend
has all the power of traveling between Fs through the continuation callback and implementors of raise
can just shift { Left(err) }
for example in the case of an Either instanceraulraja
09/06/2020, 9:59 AMYoussef Shoaib [MOD]
09/06/2020, 10:03 AMYoussef Shoaib [MOD]
09/06/2020, 10:04 AMraulraja
09/06/2020, 10:06 AMraulraja
09/06/2020, 10:07 AM