我有:
第1单元:
Module1.type1,它的构造函数,以及一些接受和返回的函数type1第2单元:
open Module1 open Module3 Module2.type2,也有接受type1和type3作为参数的功能第3单元:
open Module1open Module2Module3.type3,以及依赖于它的构造函数type1type1,type2以及type3题
结果我显然得到dependency cycle: src/Module3.cmj -> src/Module2.cmj -> src/Module3.cmj了编译器的错误.在单独导入的TypeScript/JS中可以轻易实现的东西在Reason中是不可能的.怎么解决这个问题?
我真的不想改变我的程序的架构,只是为了方便编译器/模块系统的缺点.
假设我定义了以下类型:
type queueParams = {
durable: bool
};
class type amqpChannelT = [@bs] {
pub assertQueue: string => queueParams => Js.Promise.t(unit);
};
Run Code Online (Sandbox Code Playgroud)
然后调用以下内容:
channel##assertQueue("exampleQueue", {"durable": bool});
Run Code Online (Sandbox Code Playgroud)
结果是:
This has type:
{. "durable": bool}
But somewhere wanted:
queueParams (defined as
Run Code Online (Sandbox Code Playgroud)
我怎样才能传递正确的东西?为什么我传递的东西不是记录?点符号的含义是什么?
我想呈现一个HTML复选框,其选中状态由数据控制。
给出一个接收item类型的无状态组件{ label: string, checked: bool},
像这样:
let component = ReasonReact.statelessComponent("TodoItem");
let make = (~item, _children) => {
render: _self => {
<li> <input type_="checkbox" {/*looking for something like this*/ item.checked ? "checked" : "" /* doesn't compile */}/> {ReasonReact.string(item.label)} </li>
}
}
Run Code Online (Sandbox Code Playgroud)
如何根据条件将属性的存在添加checked到input标签item.checked == true?
我正在尝试了解文档:https : //reasonml.github.io/docs/en/promise
在用法部分,有:
let myPromise = Js.Promise.make((~resolve, ~reject) => resolve(. 2));
Run Code Online (Sandbox Code Playgroud)
为什么2之前有点?它是什么意思,它是做什么的?
如何在ReasonML中的列表末尾追加一个元素(相当于Array.concatJavaScript)?
运行后:
bsb -init my-react-login-app -theme react
Run Code Online (Sandbox Code Playgroud)
然后,据我了解,我运行以下命令来构建项目:
$npm run build
Run Code Online (Sandbox Code Playgroud)
但是,我得到以下错误输出:
> my-react-login-app@0.1.0 build /Users/kevinmeredith/Workspace/my-react-login-app
> bsb -make-world
Package not found: resolving package reason-react in /
File "bsconfig.json", line 1
Error: package reason-react not found or built , if it is not built
Please run 'bsb -make-world', otherwise please install it
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! my-react-login-app@0.1.0 build: `bsb -make-world`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the my-react-login-app@0.1.0 build script. …Run Code Online (Sandbox Code Playgroud) 有大量语法示例类似于以下示例
Json.Encode.(object_([("type", string(m.type_)), ("label", string(m.label))]))
Run Code Online (Sandbox Code Playgroud)
这与调用有Json.Encode.object_(([("type", string(m.type_)), ("label", string(m.label))]))什么不同?何时使用一种语法或另一种语法?
我注意到ReasonML中的类型推断机制非常奇怪的行为。我有一条包含识别功能的记录。当我直接使用记录实例时,编译器不会抱怨。但是,当我将记录传递给另一个函数并尝试调用标识函数时,则键入推断会抱怨:
type idRecord('a) = {
// idFn can take any type.
idFn: 'a => 'a
};
let myRecord: idRecord('a) = {
idFn: anyVal => anyVal
};
// WORKS ABSOLUTELY FINE
let x1 = myRecord.idFn(10);
let x2 = myRecord.idFn("Something");
let runProgram = (program: idRecord('a)) => {
let _y1 = program.idFn(10);
// BOOM: ERROR
// This expression has type string but an expression was expected of type int
let _y2 = program.idFn("Something");
}
runProgram(myRecord);
Run Code Online (Sandbox Code Playgroud)
错误是:
该表达式的类型为字符串,但预期为int类型的表达式
我需要怎样使类型推断高兴地接受任何类型的参数?
我在 ReasonML 上玩了更多,发现type t以下示例中的模式匹配无法处理错误
错误:此模式匹配 t(float) 类型的值,但预期模式匹配 t(int) 类型的值 类型 float 与类型 int 不兼容
type t('a) =
| One: t(int)
| Two: t(float);
let x =
fun
| One => None
| Two => None;
Run Code Online (Sandbox Code Playgroud)
现在在某种程度上,如果这是关于函数的返回类型,这对我来说是有意义的。
我找到了一个等价问题的答案(我认为)。对于第二部分,答案似乎是忽略构造函数的绑定类型。在 ReasonML 中是否可能相同?
Ps:请在术语上迂腐地纠正我,我还在学习什么是什么。
Pps:我知道我可以通过显式键入来解决原始问题,x但我真的很喜欢fun它的语法,因为它很有趣。
reason ×10
ocaml ×6
bucklescript ×2
reason-react ×2
types ×2
char ×1
gadt ×1
list ×1
polymorphism ×1
promise ×1
reactjs ×1
record ×1
string ×1
syntax ×1