3

I think newbies are going to be confused by 'do' and I wonder about it from a language design standpoint. You don't want to confuse newbies at this stage of the life of a new language where pretty much everyone is a newbie and you want newbies in order to build a community and critical mass ;-)

The documentation for 'do' (3.8.3. To do or not to do) says:

There is a very good reason for this construction: in Opa, every function definition (and more generally every value not at toplevel) ends with one value, which is the result of the function — conversely, once we have reached the first value, we have the result of the function, so the function is complete.

It's the part I bolded above that I wonder about: why is it that after reaching the first value the function is complete? Was 'do' introduced in order to avoid things like this that you see in OCaml?:

let _ = (some expression)

What are the alternatives to this use of 'do' in Opa's language design? How else could this have been approached (from a language design standpoint).

4

2 に答える 2

6

明確な答えはありません。 do現在の OPA 構文では必要ですが、別の哲学を選択することもできました。

例えば:

user_update(x) =
  line = <div>
     <div>{x.author}:</div>
     <div>{x.text}</div>
  </div>
  do Dom.transform([#conversation +<- line ])
  Dom.scroll_to_bottom(#conversation)

doDom.transform関数がその行で終了するのではなく、次の行で終了することを知るために必要です。あなたが引用した本に書かれているように、「...最初の値に達すると、関数の結果が得られるので、関数は完了します。」

しかし、js のような構文を使用すると、次のようになる可能性があります。

user_update(x) {
  line = <div>
     <div>{x.author}:</div>
     <div>{x.text}</div>
  </div>;
  Dom.transform([#conversation +<- line ]);
  Dom.scroll_to_bottom(#conversation)
}

OPA 構文についてはすでに多くのフィードバックと提案を受け取っており、最適なアプローチを見つけようとしています。詳細については、次の記事を参照してください。

OPA 構文のクラウドソーシング http://dutherenverseauborddelatable.wordpress.com/2011/05/30/crowdsourcing-the-syntax/

コメントも読んでください。;)

于 2011-07-13T20:44:51.873 に答える
2

これは非常に良い質問ですが、良い答えがあるかどうかはわかりません。でもやってみよう。

まず、立場を逆転させて質問をさせてください。なぜ do は混乱するのでしょうか?

実際、状況は Ocaml と非常によく似ています。また、最後の値は関数の戻り値です。ローカルバインディングが必要になる前に何かを「実行」したい場合、let x = ... in ...または void を返す関数の場合は、セミコロンを使用する必要がありますexpr1; expr2

Opa 構文を作成している間、セミコロンはあまり好きではありませんでした;)。そう:

  • Opa では、最後の値も関数の戻り値であり、
  • 前にローカル バインディングを導入することもできますx = ...(そのため、Ocaml よりも冗長です: you don't writeletと don't write in)
  • 代わりにセミコロンを使用せず、最後の前の void 型の式を導入するので、Opadoの Ocamlは次のようになります (注意してください、通常は新しい行を追加します)。e1; e2do e1 e2e2

したがって、ここでは Ocaml と比較して根本的な変更はないと思います。しかし、実際に Cedrics が上でコメントしたように、Opa の構文についてさまざまなフィードバックが寄せられており、それに対処するための最善の方法を見つけようとしています (アイデアを歓迎します)。

于 2011-07-13T20:57:42.220 に答える