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

aneccodeal
  • 8,531
  • 7
  • 45
  • 74

2 Answers2

6

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

ire_and_curses
  • 68,372
  • 23
  • 116
  • 141
Cédrics
  • 1,974
  • 11
  • 13
  • Or it could be Python/Coffeescript like and rely on indentation... But at least I think I see the problem now - it's a syntactical issue (and it seems more horrid now that you've explained it). I'd definitely prefer either JS like (blocks) or Python-like (indentation) over the do syntax. – aneccodeal Jul 13 '11 at 21:20
  • 1
    F# (which is based on OCaml) has this concept, except it's called `ignore` rather than `do`. However, it's only needed when you call a function that returns a value, and you don't want to immediately return that value from your function - instead you "ignore" that value and keep going. Functions that don't return anything (that is to say, they return `unit`) don't need to be ignored. – Joel Mueller Jul 13 '11 at 22:49
2

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

akoprowski
  • 3,297
  • 1
  • 18
  • 26
  • You guys have got some nice ideas in Opa - don't mar it with this strange 'do' syntax. It's the only language I've ever heard of that uses 'do' this way (to indicate a function). Go with something more syntactically conventional: use indentation or {}... or even use semicolons. Any of those choices would be preferable. The current 'do' syntax is going to bite too many people and it's not very readable either. – aneccodeal Jul 13 '11 at 21:27
  • 1
    I'll add that sometimes it's the little things that effect language adoption. This certainly falls into the category of "syntactic little-things" but it could be annoying enough to hamper adoption of your language. – aneccodeal Jul 13 '11 at 21:31