▲ | manoDev 5 days ago | ||||||||||||||||
I'm sure there's merit to the language, but the syntax seems absolutely alien to me. Some attempt to look like verbose imperative code, a bunch of semicolons, and for some strange reason, hate of parenthesis. Real life sample:
A function is defined as:
That seems pretty hard to read at a glance, and easy to mistype as a definition.Also, you need to end the declaration with `in`? Then, semicolons...
... and even double semicolons ...
That looks like a language you really want an IDE helping you with. | |||||||||||||||||
▲ | myaccountonhn 5 days ago | parent | next [-] | ||||||||||||||||
That code is probably some of the hardest you'll encounter in Ocaml, but for me its quite obvious what it does and easy to read because I've worked with GADTs before. If you haven't then its you'll need to study and understand them to understand the code. I actually really like the syntax of OCaml, its very easy to write and when you're used to it, easy to read (easier than reasonml IMO). Double semicolons are afaik only used in the repl. | |||||||||||||||||
▲ | jallmann 4 days ago | parent | prev | next [-] | ||||||||||||||||
> That seems pretty hard to read at a glance, and easy to mistype as a definition. YMMV but let expressions are one of the nice things about OCaml - the syntax is very clean in a way other languages aren't. Yes, the OCaml syntax has some warts, but let bindings aren't one of them. It's also quite elegant if you consider how multi-argument let can be decomposed into repeated function application, and how that naturally leads to features like currying. > Also, you need to end the declaration with `in`? Not if it's a top level declaration. It might make more sense if you think of the `in` as a scope operator, eg `let x = v in expr` makes `x` available in `expr`. > Then, semicolons... Single semicolons are syntactic sugar for unit return values. Eg,
is the same as
| |||||||||||||||||
▲ | cmrdporcupine 4 days ago | parent | prev | next [-] | ||||||||||||||||
I think that wouldn't be too foreign to anybody who has worked for a while in Rust? It's definitely more compact and expressive than the Rust equivalent, though. This doesn't look or feel all that different to me:
| |||||||||||||||||
▲ | javcasas 5 days ago | parent | prev | next [-] | ||||||||||||||||
`let` defines a value. Some values are "plain old values", some are functions. The syntax is `let <constantname> <parameters> = <value> in <expression>;;` where `expression` can also have `let` bindings. So you can have
| |||||||||||||||||
| |||||||||||||||||
▲ | yawaramin 4 days ago | parent | prev | next [-] | ||||||||||||||||
> the syntax seems absolutely alien to me Syntax is one of those funny things. The more you look at it, the more sense it makes. The more you learn about it and the reasons behind why it's designed that way–the more sense it makes. > Also, you need to end the declaration with `in`? Not all of them, just the ones that need to refer to previously defined bindings. So eg you could do:
| |||||||||||||||||
▲ | javcasas 5 days ago | parent | prev | next [-] | ||||||||||||||||
It also uses double semicolon because single semicolon is already used as statement separator kinda like C. So double semicolon is top-level statement terminator. | |||||||||||||||||
▲ | brooke2k 4 days ago | parent | prev | next [-] | ||||||||||||||||
As with all languages, the syntax seems strange at first until you get used to it, at which point you hardly think about it. | |||||||||||||||||
▲ | UncleOxidant 5 days ago | parent | prev [-] | ||||||||||||||||
I mean, I guess it depends on your background, but that code looks pretty nice compared to how it would look in a language without pattern matching and ADTs. This is why the MLs excel for things like parsers, interpreters, and compilers. Beauty is in the eye of the beholder, I guess. I suspect that if you gave it a bit of time it would start to really grow on you - that's how it was in my case. At first: "WTF is this weird syntax?!", a few weeks in "oh, this makes a lot of sense, actually", a few years "Yeah, I'd much rather write this sort of thing in OCaml (or an ML in general)" | |||||||||||||||||
|