Use of the Apply method in Scala

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. About the protocol in Python is quite a lot, look at the Python object protocol, there are many, if the Python object agreement to understand (do not know can click here) more profound, in fact, Scala's apply method is also very well understood, for example, Scala for the DSL There is such a convention in the method invocation:

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"


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.


I feel that this is a bit like the callable object protocol in Python, which defines the __call__ method of a class and can also get similar functionality.

Of course, there must be differences between the two languages, but I think the idea of the design should be similar. Here are some examples to understand.

1. For example, numeric thearray, the operation of the first element of the array thearray (0) will be converted to thearray.apply (0) operation, which can also explain 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") operations, such as the value operation of the Map, for a simple example:

Class SomeClass {def apply (key:string): String = {println ("apply method called, key is:" + key) "Hell o world! "}} Val anyobject = new Someclassprintln (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

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:

Val numnames = array.apply ("zero", "one", "both")

The same example of a singleton object also explains the invocation of the associated object's apply method.

Object EMail {def apply (user:string, domain:string): String = {println ("apply method called") User + "@" + domain} val email = email ("Fantasia", "Sina.com") println (email)

The result of the above code execution is:

Apply method called

[Email protected]


Object apply is a more common use. Mainly used to solve the problem of initialization of complex objects , but also a singleton.

This article is from the "11016142" blog, please be sure to keep this source http://11026142.blog.51cto.com/11016142/1903436

Use of the Apply method in Scala

Related Article

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.