2017-07-03 3 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); 
}); 

私は2番目[@bs.this]のために行と列の構文エラーを取得しています。

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

私は@bs.thisのBuckleScriptドキュメントに従っています。

BuckleScriptを使用してReasonでOCamlと異なるthisをバインドする構文はありますか?私はthisを使用していますJSを生成する理由に[@bs.this] BuckleScript属性を使用するにはどうすればよい

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)) 

:BuckleScriptと、次のOCaml(ない理由は)正しい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

にコンパイルされますツールを推論するリンクいただきありがとうございます –

関連する問題