Repository navigation
Generic type parameter problem/limitation/question #4967
Description
Activity
from a cursory look, it seems that
flatMapshould be defined as*:export interface Iterable<K, V> { flatMap( mapper: (value?: V, key?: K, iter?: this) => this, context?: any ): this; }
this way no need to infer any new type parameters, just pass them through, which seems to be the expected behavior of flatMap if understand the documentation correctly.
*I am refereing to newthistypes from #4910- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on Oct 21, 2015 Paul Jolly (@myitcv) please reopen if this does not address the issue.
Mohamed Hegazy (@mhegazy) apologies, somehow I totally missed your reply!
Yes #4910 does help for certain functions.
However for
mapandflatMap,thiscan't be used as the return type because those methods are themselves generic:export interface Iterable<K, V> { map<M>( mapper: (value?: V, key?: K, iter?: this) => M, context?: any ): Iterable<K, M>; // can't use 'this' here }
Any thoughts?
No, you're not missing something obvious. Typescript doesn't support higher-kinded types. Code generation might be the answer if you can't express the concept any other way.
Haskell's
Functortypeclass is essentially what you want, but with more methods than justmap.The type is
fmap :: Functor f => (a -> b) -> f a -> f b-- replaceFunctorwithIterableand you getfunction fmap<F extends Iterable, A, B>(mapper: (a:A) => B, i: F<A>): F<B> { // code that uses the iterable interface to iterate over i... }
Unfortunately, Typescript doesn't support higher-order types, so you'll have to find a different way of expressing this code.
Nathan Shively-Sanders (@sandersn) - thanks for confirming. Code generation is probably the cleanest solution here.
Unfortunately this means no Functor
mapnor generic iterator factories.- locked and limited conversation to collaborators
on Jun 19, 2018
Consider the following which is a modified cut-down version of the
immutable-jstype definition:The existence of the type parameter
Tfor the definitions ofIIterableandISeqallowsslice()to reflect that it returns a sliced instance of the same container 'type' without requiring a type assertion:However I can't type
flatMapandflip. Taking flip as an example, it should return a container of the same 'type' but withKandVswitched.flatMapshould return a container of the same 'type' but withMKandMV.This problem would be 'solved' if we could refer to generic types without requiring that they be passed arguments:
Am I missing something obvious here?
Or do I need to look to some higher order definition with code generation of (generic) interfaces to properly support this?