Explore Scala (6) -- tuples

Source: Internet
Author: User

This article discusses the usage and implementation of tuple.

Unit

Scala language does notVoidKeyword, instead, yesUnitConcepts (and objects ). Scala is more oo than Java, which is one of the following aspects. From the perspective of Scala, unit and tuple are not very closely related, but for two reasons, I plan to discuss unit first:

  1. Similar concept: tuple indicates the object that contains n (n> 0) elements, Unit indicates no object, or no object is required. Therefore, unit can be considered as a special case of tuple, that is, the tuple that contains 0 elements.
  2. Syntax: You can use parentheses to create a tuple instance. For example, Val t = (1, "2", false) creates a triple. A pair of null parentheses creates a unit instance, such as Val u = ()

Most of the time, unit is justConceptIt is equivalent to Java'sVoidKeyword. For example, the following scala code:

def unitIsVoid(x: Int, y: Int): Unit = {    val z = x + y}
After decompilation, the following Java code is actually obtained:

public void unitIsVoid(int x, int y) {    int z = x + y;}
However, if you have to use a unit instance, it works as well:

def unitIsNotVoid(nothing: Unit) = {    val unit = ()    if (unit == nothing) {        println("unit == nothing")    }}
The scala code above can be compiled, but the compiler will give a warning:

Warning: comparing values of types unit and unit using '= 'will always yield true

The following is the decompiled Java code:

public void unitIsNotVoid(scala.runtime.BoxedUnit nothing) {    scala.runtime.BoxedUnit unit = BoxedUnit.UNIT;    if (unit.equals(nothing)) {        ...    }}
As you can see, the embodiment of the unit concept is the boxedunit. Unit singleton object.

Tuple1

Tuple1 is the tuple that contains only one element, that is, the mona1 group. Note that tuple1 cannot be used.ParenthesesYou must use tuple1 ("X") to instantiate the instance, as shown in the following code:

def tuple1() = {    val x = ("a")    val y = Tuple1("b")}
The following is the decompiled Java code:

String x = "a";Tuple1 y = new Tuple1("b");

Tuplen

Can a tuple containing n elements be expressed at will? The answer is:No. Scala only defines tuple1 ~ Tuple22 has a total of 22 tuple (in the scala package), and the Circular brackets only support tuple1 ~ Tuple22 is not supported when there are more. For example, the following code cannot be compiled:

def tuple23() = {    val t23 = (1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,"21","22","23")}
Scala compiler reports the following error:

Error: Object <none> is not a member of package Scala

The function returns multiple values.

Tuple has many functions. Tuple is suitable for methods that want to return multiple values but are not worth defining a class separately. The following is an example:

def nameAndAge() = {    ("zxh", 32)}

Assign a tuple value to multiple variables.

You can also assign tuple to n variables, which is equivalent to removing tuple. The Code is as follows:

def assignToVars() = {    val (name, age) = nameAndAge()    println(name + " is " + age + " old")}

Instantiate Map

The following code instantiate a map:

val m = Map("a" -> 1, "b" -> 2)
Scala seems to support map literal, but it is not actually. The following is the decompiled Java code:

Tuple2[] ts = new Tuple2[2];ts[0] = scala.Predef$ArrowAssoc$.MODULE$.$minus$greater$extension("a", 1); // ->ts[0] = scala.Predef$ArrowAssoc$.MODULE$.$minus$greater$extension("b", 2);scala.collection.immutable.Map$.apply(ts);
-> Actually only Implicit conversionMethod, which is defined as follows:

object Predef {    implicit final class ArrowAssoc[A](private val self: A) extends AnyVal {        @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(self, y)    }}
That is to say,-> convert the key and value to tuple2, and then pass the tuple2 array to the apply method of the map singleton object.


Explore Scala (6) -- tuples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.