Python Pass equivalent in Scala

If there is a function you don’t want to do anything with, try doing something like this in Python:

def f(): pass 

My question is: is there something similar to pass in Scala?

+6
source share
4 answers

pass is the syntax quirk of Python. There are some cases where the grammar requires you to write an instruction, but sometimes you do not need an instruction. Why pass : is an operator that does nothing.

Scala never requires you to write instructions, so the way to not write instructions is simply not to write instructions.

+9
source

I think () is similar.

 scala> def f() = () f: ()Unit scala> f scala> 
+5
source

Typically, you can replace Unit with pass . You do nothing, and the line evaluates to Unit , like Java void , but itself explicit.

 // implicit return value of type Unit def showMsg(msg:Option[String]) = msg match { case None => Unit case Some(m) => println(m) } 
+2
source

As I understand it, python pass used for cases not yet implemented. If you need such a thing in scala, then use ??? it is similar to () , but is a single instance of Nothing. Your code will be compiled, but if you call such a method, it will work with NotImplementedError

 def foo: ResultType = ??? 
+1
source

Source: https://habr.com/ru/post/954222/


All Articles