If there is a function that you don't want to do anything with you simple do something like this in Python:
def f():
pass
My question is, is there something similar to pass
in Scala?
As those examples show, Scala clearly lets you pass anonymous functions and regular functions into other methods. This is a powerful feature that good FP languages provide.
The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements.
In Python, the pass keyword is an entire statement in itself. This statement doesn't do anything: it's discarded during the byte-compile phase. But for a statement that does nothing, the Python pass statement is surprisingly useful. Sometimes pass is useful in the final code that runs in production.
pass
is a syntactic quirk of Python. There are some cases where the grammar requires you to write a statement, but sometimes you don't want a statement there. That's what pass
is for: it's a statement that does nothing.
Scala never requires you to write a statement, therefore the way to not write a statement is simply to not write a statement.
I think ()
is similar.
scala> def f() = ()
f: ()Unit
scala> f
scala>
As i understand in python pass
is used for not yet implemented cases. If you need such thing in scala then use ???
it's similar to ()
, but is a function returning Nothing (def ??? : Nothing = throw new NotImplementedError
) . Your code will compile, but if you call such a method it will crash with NotImplementedError
def foo: ResultType = ???
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With