1

I am currently learning Scala using a tutorial, and I have come across a syntax I do not understand (and I haven not found the answer):

object Demo {
   def main(args: Array[String]) {
     println(apply(layout, 10))
   }

   def apply(f: Int => String, v: Int) = f(v)

   def layout[A](x: A) = "[" + x.toString() + "]"
}

In

def layout[A](x: A) = "[" + x.toString() + "]"

I do not understand the [A] after layout and before the argument declaration.

Is it the return type?

For me the general syntax for a function in scala is the following;

def functionName ([list of parameters]) : [return type] = {
   function body
   return [expr]
}

3 Answers 3

4

A is something called a type parameter. Type parameters allow you to write a method, generically, for any A. It could be that A is an Int, Double, or even a custom class you've written. Since all of these have a toString method inherited from Any, this will work.

For example, when we do:

println(layout(1L))
println(layout(1f))

This is identical to writing:

println(layout[Long](1L))
println(layout[Float](1f))

Where the type parameters are explicitly passed.

Sign up to request clarification or add additional context in comments.

4 Comments

If you know Java, this is roughly equivalent to how they annotate generic methods with <A>.
In this case, does it do anything useful, though? Why not just def layout(x: Any)?
@Thilo In this particular example, it's not too meaningful. One could use Any and achieve the same thing. When one writes a method accepting a type parameter, you usually have reusable code where you gain from keeping the type information at compile time.
@Thilo using type parameter, you can add type bounds in a more complicated scenario.
0
def layout[A](x: A) = "[" + x.toString() + "]"

This [A] here is the type parameter. This function definition allows you to provide any type as argument for this type parameter.

// If you wanted to use an Int
layout[Int](5)

// If you wanted to use a String
layout[String]("OMG")

// If you wanted to one of your classes
case class AwesomeClass(i: Int, s: String)

layout[AwesomeClass](AwesomeClass(5, "omg"))

Also... In this method def layout[A](x: A) = "[" + x.toString() + "]", it is specified that the function parameter x is of type A, Scala can use this information to infer the type parameter from the function parameter x.

So you actually do not need to provide the type argument when using the method, so you can actually write the above code in a less verbose manner like following,

// As we provided `i : Int` as argument `x`,
// Scala will infer that type `A` is `Int` in this call
val i: Int = 5
layout(i)

// Scala will infer that type `A` is `String` in this call
layout("OMG")

// If you wanted to one of your classes
case class AwesomeClass(i: Int, s: String)

// Scala will infer that type `A` is `AwesomeClass` in this call
layout(AwesomeClass(5, "omg"))

Comments

-1

I made this diagram below in order to generalize the syntaxes for constants, variables and functions.

Syntax combinations to declare Constants, Variables and Functions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.