1)有没有办法输入这个?2)任何人都能够解释这些错误消息?
let identity1: 'a => 'a = [%bs.raw {|
function(value) {
return value
}
|}];
/*
Line 2, 11: The type of this expression, '_a -> '_a, contains type variables that cannot be generalized
*/
let identity2: 'a. 'a => 'a = [%bs.raw {|
function(value) {
return value
}
|}];
/*
Line 8, 11: This definition has type 'a -> 'a which is less general than 'a0. 'a0 -> 'a0
*/
Run Code Online (Sandbox Code Playgroud)
免责声明:我是ReasonML初学者.
我最近开始使用ReasonML,我发现与vanilla JavaScript相比,性能有很大差异.这是我反对简单解谜功能的例子(谜题取自:https://adventofcode.com/2015/day/1)
ReasonML
let head = str =>
switch (str) {
| "" => ""
| _ => String.sub(str, 0, 1)
};
let tail = str =>
switch (str) {
| "" => ""
| _ => String.sub(str, 1, String.length(str) - 1)
};
let rec stringToList = str =>
switch (str) {
| "" => []
| _ => [[head(str)], tail(str) |> stringToList] |> List.concat
};
let rec findFloor = code =>
switch (head(code)) …Run Code Online (Sandbox Code Playgroud) 假设我有一个名为 的全局对象Example,它有一个构造函数,该构造new Example()函数返回一个带有名为 的键的对象"Messaging"
假设我有以下原因/Bucklescript 代码:
[@bs.deriving abstract] type example = {
[@bs.as "Messaging"] messaging: string
};
type wrappedExample = Js.t(example);
[@bs.new] external exampleConstructor: unit => wrappedExample = "ExampleThing";
exampleConstructor()#messaging;
Run Code Online (Sandbox Code Playgroud)
这导致:
This expression has type wrappedExample
It has no method messaging
Run Code Online (Sandbox Code Playgroud)
将最后一行更改为:
exampleConstructor()##messaging或exampleConstructor().messaging同样失败。这里有什么问题呢?我如何访问我的 javascript 值?
我正在尝试了解文档:https : //reasonml.github.io/docs/en/promise
在用法部分,有:
let myPromise = Js.Promise.make((~resolve, ~reject) => resolve(. 2));
Run Code Online (Sandbox Code Playgroud)
为什么2之前有点?它是什么意思,它是做什么的?
我正在研究基于这个项目的 bucklescript 绑定到leafletjs 。
使用传单,地图具有添加图层的功能,而图层具有将自身添加到地图的功能。
这就是我想用 ReasonML 实现的目标:
module Map = {
type t;
[@bs.send] external addLayer : (t, Layer.t) => t = "addLayer";
};
module Layer = {
type t;
[@bs.send] external addTo : Map.t => unit = "addTo";
};
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到一个未绑定的模块层错误。
如何让编译器知道后面描述的类型?
考虑到以下人为的示例,是否可以编写一个get可以处理具有a属性的任何记录的函数?
type type_one = {a: int}
type type_two = {a: int, b: int}
let example_one = {a: 1}
let example_two = {a: 1, b: 2}
let get = record => record.a
Js.log(get(example_one)) // notice the error here
Js.log(get(example_two))
Run Code Online (Sandbox Code Playgroud)
如果不是,这可以用对象实现吗?或者,处理这种情况的最佳方法是什么?
假设我有这个对象的JSON数组:
[
{"name": "foo", "tags": ["bird", "animal"], "age": 10},
{"name": "bar", "tags": ["dog", "animal"], "age": 5},
{"name": "baz", "tags": ["cat", "animal"], "age": 3}
]
Run Code Online (Sandbox Code Playgroud)
如何在ReasonML中对此进行解码?
我有 let intervalId = option(Js.Global.intervalId)
我想以一种简洁的方式对Js.Global.clearInterval选项是否具有值进行副作用调用(即is Some(id)和not None)
也许Belt.Option.map功能就是答案,但是我在使用它时遇到了问题。
我是OCaml和ReasonML的新手,但是我知道的几种语言都有合适的功能。我在这里释义其中的一些,以给出我想要的想法:
在斯卡拉,我会说: intervalId.foreach(Js.Global.clearInterval)
在Swift中,我会说: intervalId.map(Js.Global.clearInterval)
我正在尝试存档一个函数正在返回一个 unicode 字符串,我想将它登录到控制台。但它要么显示有关函数的信息,要么不显示 unicode 字符串。
let test = () => "ÜTEST"
Js.log(test());
Js.log({j|$test()|j});
Run Code Online (Sandbox Code Playgroud)
第一个只返回"ÃTEST",第二个只返回有关函数本身的信息。
这是一个工作示例:https : //reasonml.github.io/en/try?rrjsx=true&reason=FAGwpgLgBBYM7QLxQBQEoqIHxQEQB2AVAUQGVDdhgApOAOhAHsBzFWBdNAbitoZZQBvAFYAfACTsI6UcIC+3
bucklescript ×9
reason ×9
ocaml ×4
javascript ×2
dependencies ×1
ffi ×1
json ×1
module ×1
optional ×1
performance ×1
promise ×1
rescript ×1
unicode ×1