Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Python's Pass in Scala

Tags:

python

scala

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?

like image 540
Games Brainiac Avatar asked Sep 20 '13 05:09

Games Brainiac


People also ask

Is there a 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.

What is Python pass keyword?

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.

Does Pass do anything in Python?

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.


3 Answers

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.

like image 70
Jörg W Mittag Avatar answered Oct 12 '22 00:10

Jörg W Mittag


I think () is similar.

scala> def f() = ()
f: ()Unit

scala> f              

scala>
like image 10
Brian Avatar answered Oct 12 '22 02:10

Brian


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 = ???
like image 4
4lex1v Avatar answered Oct 12 '22 00:10

4lex1v