Scala's apply

Source: Internet
Author: User

Scala is a static type of scripting language built on the JVM, and scripting languages always have some conventions to enhance flexibility. Flexibility allows the person who has mastered it to be a duck in the hand, as well as a beginner. For example, Scala has such a convention in the method invocation for the DSL to mate:

In the case where the receiver of the method call is defined, the point and parenthesis can be omitted when the method has only one argument. such as "0 to 2", the actual full call is "0.to (2)". However, "println (2)" cannot be written as "println 10" because the receiver console of the method call is not written, so it can be written as "console println 10"

This is where the conventions related to the apply and update methods are described, in the case of code directly appended to the object (object), that is:

When you pass parentheses to one or more arguments to a variable (object), Scala converts it to a call to the Apply method, and as such, when you assign a value with parentheses and include one to several parameters, the compiler uses the object's Update method to invoke the arguments in parentheses and the object to the right of the equals sign.

The explanation of the above paragraph can be well understood by the following examples.

1. TheArray (0), the operation of the first element of the array is converted to the thearray.apply (0) operation, which also explains why the Scala array value is not enclosed in parentheses, because it is also a method call

2. Anyobject ("Key1") will be converted to anyobject.apply ("key") operation, which will also let us think of the MAP value operation, indeed. We can give an example:

1 2 3 4 5 6 7 8 9 Class SomeClass {def apply (key:string): String = {println ("apply method called, key is:" + key)      "Hello world!" }} val anyobject = new SomeClass println (Anyobject ("Key1"))

The output after execution is:

Apply method called, key Is:key1
Hello world!

The description is called to the appropriate apply method.

3. When constructing an Array or MAP, we will simply write

1 Val numnames = Array ("zero", "one", "one")

Here is also the invocation of the Apply method, we seem to be acting on the class array, but not, but in the array of the associated object (object array), called the associated object array of the Apply method, that is:

1

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.