Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala and Python's pass

Tags:

scala

I was wondering, is there an equivalent of python's pass expression? The idea is to write method signatures without implementations and compiling them just to type-check those signatures for some library prototyping. I was able to kind of simulate such behavior using this:

def pass[A]:A = {throw new Exception("pass"); (new Object()).asInstanceOf[A]}

now when i write:

def foo():Int = bar()
def bar() = pass[Int]

it works(it typechecks but runtime explodes, which is fine), but my implementation doesn't feel right (for example the usage of java.lang.Object()). Is there better way to simulate such behavior?

like image 715
Arg Avatar asked Dec 07 '12 12:12

Arg


People also ask

Is there a pass in Scala?

The UNDER30 Pass brings you into the world of La Scala. The UNDER30 Pass is reserved to people born from 1992 onwards.

What is Python pass?

Python pass Statement 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.


1 Answers

In Scala 2.10, there is the ??? method in Predef.

scala> ???
scala.NotImplementedError: an implementation is missing
  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:252)
  ...

In 2.9, you can define your own one like this:

def ???[A]:A = throw new Exception("not implemented")

If you use this version without an explicit type paramter, A will be inferred to be Nothing.

like image 163
Kim Stebel Avatar answered Oct 02 '22 11:10

Kim Stebel