2017-07-03 69 views
1

我想写ReasonML编译为这个JS:如何在Reason(ReasonML)中使用[@ bs.this] BuckleScript属性?

function main(example) { 
    example.foo = function() { 
     console.log(this) 
    } 
} 

这里是我的原因:

let module Example = { 
    type t; 
    external set_foo_method : t => (t => unit [@bs.this]) => unit = "foo" [@@bs.set]; 
}; 

let main = fun example => Example.set_foo_method example (fun [@bs.this] x => { 
    Js.log(x); 
}); 

我得到的行和列的语法错误第二[@bs.this]

File "/Users/maxwellheiber/dev/rerect/src/demo.re", line 6, characters 62-64: 
Error: 742: <SYNTAX ERROR> 

我正在关注@bs.this的BuckleScript文档。

与OCaml相比,使用BuckleScript绑定到this的原因与原因不同吗?下面OCaml的(而不是理性)与BuckleScript属性没有错误编译到正确的JS:

module Example = struct 
    type t 
    external set_foo_method : t -> (t -> unit [@bs.this]) -> unit = "foo" [@@bs.set] 
end 

let main example = Example.set_foo_method example (fun [@bs.this] x -> Js.log(x)) 

如何使用[@bs.this] BuckleScript属性的原因,产生使用this JS?

回答

3

是的,属性优先,这是不幸的微妙不同。 Reason Tools(这是伟大的转换小片段是这样)说,这是你想要什么:

module Example = { 
    type t; 
    external set_foo_method : t => (t => unit) [@bs.this] => unit = "foo" [@@bs.set]; 
}; 

let main example => Example.set_foo_method example ((fun x => Js.log x) [@bs.this]); 

它将编译为

function main(example) { 
    example.foo = (function() { 
     var x = this ; 
     console.log(x); 
     return /*() */0; 
    }); 
    return /*() */0; 
} 
+0

感谢您对理性的工具链接 –