Thinking about control structures vs functions

Hello. I am thinking about two approaches to programming in scala:

  1. Making everything possible to control abstraction (using {} like .map { (x, y) => x * y } instead of .map( (x, y) => x * y )

  2. Using only built in control abstraction (or maybe even write if similiar to haskell function if' :: Bool -> a -> a -> a)

Can’t we think about if-else as function as much as map or filter function? How do you feel seeing
map { (x, y) => x * y } instead of map( (x, y) => x * y ) ?
I can’t see the difference. I especially don’t understand it since scala 3 no longer requires {} in if-else and try-catch. How can I think about control structures vs functions?

To put multiple expressions or statements in the body of a function literal passed to map, you need curly braces, but if it’s just a single expression, as in your example, it really doesn’t matter either way. It’s a difference I don’t even really notice when reading Scala code. I suspect that even if you set some kind of personal policy about it in your own code, your readers won’t notice.

How can I think about control structures vs functions?

Control structures 1) can have special syntax, and 2) are especially likely to be familiar to your readers. I don’t think see a deep difference beyond that.

As for Scala 3, hopefully the experimental fewerBraces option gets finished and released as non-experimental. It’s documented at Optional Braces | Scala 3 Language Reference | Scala Documentation

1 Like