我有一个类型相互关联的元组。在我的情况下,它是一个提取函数,它提取一个值,该值又用作另一个函数的输入。
从概念上讲,我正在寻找的东西是这样的,但这不能编译:
const a: <T>[(v:any) => T, (t:T) => void] = [ ... ]
Run Code Online (Sandbox Code Playgroud)
用例是这个。我有一个类型为的传入RPC消息any,以及一个具有众所周知的参数类型的API。我想构建一个包含两个参数的“接线计划”,一个是提取器函数,另一个是对应的API函数。
export interface API = {
saveModel : (model:Model) => Promise<boolean>,
getModel : (modelID:string) => Promise<Model>,
}
const api: API = { ... }
// this is the tuple type where i'd like to define that
// there's a relation between the second and third member
// of the tuple.
type WirePlan = [[string, (msg:any) => T, (t:T) => Promise<any>]]
const wirePlan: WirePlan = …Run Code Online (Sandbox Code Playgroud) 我最近问过如何制作一个GADT实例的同源列表:函数返回 GADT 的任何构造函数的结果
TL;博士
{-#LANGUAGE GADTs, EmptyDataDecls #-}
module Main where
-- Define a contrived GADT
data TFoo
data TBar
data Thing a where
Foo :: Int -> Thing TFoo
Bar :: String -> Thing TBar
data SomeThing = forall a. SomeThing (Thing a)
combine :: [SomeThing]
combine = [Something $ Foo 1, SomeThing $ Bar "abc"]
Run Code Online (Sandbox Code Playgroud)
现在,我在动态"展开"它们时遇到了一些麻烦.假设我们有这个(仍然是人为的,但更接近我的实际用例)代码:
{-#LANGUAGE GADTs, EmptyDataDecls #-}
module Main where
-- Define a contrived GADT
data Thing a where
Thing :: TKind a -> …Run Code Online (Sandbox Code Playgroud)