如何为联合类型编写 reasonml 绑定
How to write reasonml binding for a union type
我正在尝试为 https://github.com/oblador/react-native-keychain/blob/master/typings/react-native-keychain.d.ts#L76
编写绑定
getGenericPassword returns false
如果出错,否则 object
(credentials
)。我不确定这种联合类型是否可以合理地表示,但更好的 API 将是一个选项 (option(credentials)) 的结果。但是,如何在绑定文件中转换 Promise<boolean | credentials>
-> Js.Promise.t(option(credentials))
。下面是一个模板。
感谢您的帮助。
[@bs.deriving abstract]
type credentials = {
service: string,
username: string,
password: string,
};
/* TODO convert the actual return value
Js.Promise.t(option(credentials)) to more reason type
Js.Promise.t(option(credentials)) */
[@bs.module "react-native-keychain"] [@bs.scope "default"]
external getGenericPassword: unit => Js.Promise.t(option(credentials)) = "";
您可以使用 Js.Types.classify
获取值的运行时类型。
type maybeCredentials;
[@bs.module "react-native-keychain"] [@bs.scope "default"]
external getGenericPassword: unit => Js.Promise.t(maybeCredentials) = "";
let getGenericPassword: unit => Js.Promise.t(option(credentials)) =
() =>
Js.Promise.(
getGenericPassword()
|> then_(maybeCredentials =>
switch (Js.Types.classify(maybeCredentials)) {
| JSObject(obj) => resolve(Some(obj |> Obj.magic))
| _ => resolve(None)
}
)
);
这里定义了maybeCredentials
作为中间类型使用
然后我们定义一个与绑定同名的函数,它将 "shadow" 名称并防止直接使用绑定以支持我们的 "override"。但是,在覆盖范围内我们仍然可以使用绑定。
然后我们调用 Js.Types.classify
来获取 returned 值的运行时类型。如果它是一个对象,我们使用 Obj.magic
将抽象 obj_type
转换为我们的 credentials
类型(从函数的 return 类型推断),并将其包装在 option
。对于任何其他类型,我们 return None
.
顺便说一句,这种"type"被称为无标签联合。作为生产者和消费者,我在 bucklescript-cookbook.
中写下了一些使用不同策略处理这些问题的示例
我正在尝试为 https://github.com/oblador/react-native-keychain/blob/master/typings/react-native-keychain.d.ts#L76
编写绑定getGenericPassword returns false
如果出错,否则 object
(credentials
)。我不确定这种联合类型是否可以合理地表示,但更好的 API 将是一个选项 (option(credentials)) 的结果。但是,如何在绑定文件中转换 Promise<boolean | credentials>
-> Js.Promise.t(option(credentials))
。下面是一个模板。
感谢您的帮助。
[@bs.deriving abstract]
type credentials = {
service: string,
username: string,
password: string,
};
/* TODO convert the actual return value
Js.Promise.t(option(credentials)) to more reason type
Js.Promise.t(option(credentials)) */
[@bs.module "react-native-keychain"] [@bs.scope "default"]
external getGenericPassword: unit => Js.Promise.t(option(credentials)) = "";
您可以使用 Js.Types.classify
获取值的运行时类型。
type maybeCredentials;
[@bs.module "react-native-keychain"] [@bs.scope "default"]
external getGenericPassword: unit => Js.Promise.t(maybeCredentials) = "";
let getGenericPassword: unit => Js.Promise.t(option(credentials)) =
() =>
Js.Promise.(
getGenericPassword()
|> then_(maybeCredentials =>
switch (Js.Types.classify(maybeCredentials)) {
| JSObject(obj) => resolve(Some(obj |> Obj.magic))
| _ => resolve(None)
}
)
);
这里定义了maybeCredentials
作为中间类型使用
然后我们定义一个与绑定同名的函数,它将 "shadow" 名称并防止直接使用绑定以支持我们的 "override"。但是,在覆盖范围内我们仍然可以使用绑定。
然后我们调用 Js.Types.classify
来获取 returned 值的运行时类型。如果它是一个对象,我们使用 Obj.magic
将抽象 obj_type
转换为我们的 credentials
类型(从函数的 return 类型推断),并将其包装在 option
。对于任何其他类型,我们 return None
.
顺便说一句,这种"type"被称为无标签联合。作为生产者和消费者,我在 bucklescript-cookbook.
中写下了一些使用不同策略处理这些问题的示例