Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of {} in the returning type of a function?

Tags:

scala

In the following function declaration:

def f(x: Int): Int {} = x + 1

what is the purpose of {}?

The result of the method invocation is the same with or without the curly braces.

like image 594
Octavian R. Avatar asked Mar 06 '18 21:03

Octavian R.


People also ask

What does return {} mean in Python?

The Python return keyword exits a function and instructs Python to continue executing the main program. The return keyword can send a value back to the main program. A value could be a string, a tuple, or any other object. This is useful because it allows us to process data within a function.

What is return {} in Javascript?

The return statement is used to return a particular value from the function to the function caller. The function will stop executing when the return statement is called. The return statement should be the last statement in a function because the code after the return statement will be unreachable.

What does return {} mean in C++?

return {}; means that {} is the initializer for the return value. The return value is list-initialized with an empty list.

What is the purpose of returning value from function?

A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.


1 Answers

It's just an empty refinement. In Scala any type can have a refinement, that constrains the definitions of members (e.g., types, vals, defs) of the original type.

For example:

trait Base { type T }

type BaseInt = Base { type T = Int }

Those members don't have to be defined in any base type, the refined type can itself define new members. And there are no restrictions on the type being refined, it is allowed to refine AnyVal or any of its subtypes. So the following code is perfectly legal:

type A = Int { type C = Boolean; val a: String }

According to the Scala specification:

If no refinement is given, the empty refinement is implicitly added, i.e. T1 with … with Tn is a shorthand for T1 with … with Tn {}.

So in your code Int {} is the same as Int.


Also, according to the specification, two types are considered equal if their refinements match exactly. So the following code results in a compile-time error:

scala> type A = Int { type C = Boolean; val a: String }
defined type alias A

scala> val a: A = 10 
<console>:12: error: type mismatch;
 found   : Int(10)
 required: A
    (which expands to)  Int{type C = Boolean; val a: String}
       val a: A = 10
                  ^

But as those refinements don't exist at runtime due to erasure, all casts are perfectly legal, if you don't use the members from refinements:

scala> val a: A = 10.asInstanceOf[A] 
a: A = 10

This feature can be used to implement tagged types, that are the same and represented by some primitive type at runtime, but can be distinguished at compile time.

like image 143
Kolmar Avatar answered Nov 09 '22 20:11

Kolmar