Re:OCaml at First Glance
by acjohnson55 on Monday August 29, 2022 @02:52PM
(#32641996)
I think they're often helpful for reading, but I admire what Scala did in Scala 3 to eliminate their necessity.
[ Parent
| on HN ]
Scala 3 actually doesn't really eliminate braces in any meaningful way. Sure, they're optional for delimiting traits, classes, and objects. But braces are heavily used for block scoping in Scala too. E.g. a very common pattern:
retry(Seq(1.second, 2.second, 3.second)) {
// some action that can fail
}
This still requires the braces as of right now. I think the syntax changes will just confuse more people than it helps, at least for the time being.
Re:OCaml at First Glance
by AzzieElbab on Monday August 29, 2022 @04:40PM
(#32643279)
That is a curried parameter in {}. Those braces are not for scoping. They basically define a lambda. You can probably use () instead
you can also use import language.experimental.fewerBraces with nightly builds to write code like this
List(1,2,3).map: c => ...
The braces here absolutely denote a new scope. You can't bind vals inside parentheses, for example. You can inside braces.
> You can probably use () instead
I would rather not as using braces for the final argument has been the Scala idiom for a long time now.
Re:OCaml at First Glance
by AzzieElbab on Tuesday August 30, 2022 @10:10PM
(#32657542)
I see your point. However you don’t have to define that thunk inline. You can write it separately and just pass it by name retry(…)(thunk). Not sure what signature of the retry method looks like.
You can define it separately but it's often not the idiom. Especially for call-by-name arguments like in this case:
def retry[A](backoffSeconds: Seq[Int])(action: => A): A
In this case you can't just define a 'val action = ...' because you don't want it to be run immediately, you want to run it inside the 'retry' method only. You could define it as a 'def' but then you're just introducing two different delayed evaluation concepts into this small piece of code. The cleanest way is to just directly pass in the action and Scala ensures it is delayed evaluation thanks to the CBN parameter type: retry(Seq(1, 2, 3)) {
println("Trying")
}