In Scala, what exactly does 'val a: A = _' (underscore) mean?

What exactly does val a: A = _ initialize a value to? Is this a typed null? Thanks.

Answers


val a: A = _ is a compile error. For example:

scala> val a: String = _
<console>:1: error: unbound placeholder parameter
       val a: String = _
                       ^

What does work is var a: A = _ (note var instead of val). As Chuck says in his answer, this initialises the variable to a default value. From the Scala Language Specification:

0 if T is Int or one of its subrange types, 0L if T is Long, 0.0f if T is Float, 0.0d if T is Double, false if T is Boolean, () if T is Unit, null for all other types T.


Need Your Help

What is the difference between iteration and traversing?

c++ data-structures iteration terminology traversal

The past few weeks I have been learning about iterators. I still do not understand the main difference between iterating through a link list and traversing through one. I know that traversing means...

Convert SQL Server database to SQL Server CE?

c# sql-server sql-server-2008 compact-framework sql-server-ce

I have a SQL Server database which I can read from, is there a quick way I can turn it into an SQL Server CE database through C# or will I have to read in all the data then output it in SQL Server ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.