开发者

Why is 'do' needed in the Opa language?

开发者 https://www.devze.com 2023-03-20 05:53 出处:网络
I think newbies are going to be confused b开发者_运维知识库y \'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 langua

I think newbies are going to be confused b开发者_运维知识库y '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).


There is no straight answer. do is needed with the current OPA syntax, but we could have chosen another philosophy.

For example:

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

do is needed to know that the function doesn't end at the Dom.transform line, but the next one. As written in the book you quoted: "...once we have reached the first value, we have the result of the function, so the function is complete."

But with a js-like syntax it could have been:

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

We have already received plenty of feedback and suggestions for the OPA syntax, and we are trying to find the best approach. You can read this article to know more:

Crowdsourcing the OPA syntax http://dutherenverseauborddelatable.wordpress.com/2011/05/30/crowdsourcing-the-syntax/

Read the comments as well. ;)


That's a very good question to which I'm not sure I have a good answer. Let me try, though.

Let me start by turning tables and asking you a question: why is do confusing?

In fact the situation is very similar to Ocaml. There also the last value is the return value of the function. If you want to "do" something before you either need a local binding let x = ... in ... or for functions returning void you need to use semi-colon expr1; expr2.

While creating Opa syntax we were not very fond of semicolons ;). So:

  • in Opa also the last value is the return value of the function,
  • you can also introduce local bindings before x = ... (so it's more verbose than Ocaml: you don't write let and don't write in)
  • we don't use semi-colons instead the void-typed expressions before the last are introduced with do so Ocaml's e1; e2 in Opa becomes do e1 e2 (mind you, usually you'd put e2 on a new line).

So I don't think there are fundamental changes compared to Ocaml here. But indeed as Cedrics commented above, we receive mixed feedback about Opa's syntax and are trying to figure out a best way to address that (ideas welcome).

0

精彩评论

暂无评论...
验证码 换一张
取 消