Procedure syntax

I recently started reading up on Scala 3, and I like what I see.

I had suggested the Python-style indentation syntax several years ago, and I am happy to see that it will be adopted.

I am slightly disappointed that my suggestion for procedure syntax has not been adopted. I had suggested that a new keyword such as “proc” be used instead of “def” to declare a procedure. This would make it clearer that what follows is a procedure that returns no value rather than a method or function that does. And it would do so without the weird convention of returning a type of “Unit”.

To address concerns about backward compatibility, it could be made optional, allowing the user to return Unit if desired.

This is not a radically new idea since Ada has both functions and procedures.

Even if a method returns a value it can do side-effects. A value returning method can be invoked only for the side effects, most often it’s seen with various java.util.concurrent.atomic.AtomicXxx. I think that if someone really cares about controlling side-effects then he uses some sort of IO monad and in that case instead of Unit he returns IO[Unit].

1 Like

I realize that a method that returns a value can have side effects. The purpose is not to assert that a def is free of side effects. The point is that a “proc” keyword would make it clear that the function or method does not return any value. It would be clear even if the user omits the so-called “Unit” return type.

What is wrong with returning Unit? It brings unification, all functions / methods return something, even if the return is an empty tuple.

Introducing this procedure would only make things more complex, because now we would have differences between functions, methods & procedures (note that Dotty has done an excellent work to minimize the differences between the first two, thus adding a new one difference is a step backwards).
Also, how this would affect the fact that higher order functions like foreach expect a function, how would you transform a procedure into a function?

Finally, if this is just sugar syntax for omitting Unit then we are not wining too much and losing one reserved word.

1 Like

I think the “no procedure syntax” ship has already sailed.

I continue, after well over a year now, to have much more trouble picking out ): Unit = (instead of, say, ): Int = than I do ) {. So the change has unreservedly made it harder for me to read a great deal of my code.

But other people claim the opposite, and the decision was made a long time ago, so we should probably just enjoy the increased regularity that every def returns a value explicitly.

Adding an extra keyword seems like overkill.

1 Like

Apparently I’m in the minority on this one, but I still see a “proc” keyword as a useful way to distinguish between procedures that return nothing and functions or methods that return something. Aside from potential name clashes with existing code, it would be fairly trivial to implement and would enhance readability.