The effect of the 18 partial function on the default parameters of the Scala fun topic

Source: Internet
Author: User


What will be the results of the following program?


Package Pzsobject Pz018 extends APP {def invert (v3:int) (V2:int = 2, V1:int = 1) {println (v1 + "," + v2 + "," +  V3); } def INVERT3 = Invert (3) _ Invert3 (v1 = 2) invert3 (V1 = 2, v2 = 1)}















Explain

ETA expanded, missing information for default parameters


Explanation


The type of invert3 after Eta-expansion (sls§6.26.5) was no longer a method but a function object, i.e. an instance of fun Ction2[int, Int, Unit]. This was also reported by the REPL:


scala> def INVERT3 = Invert (3) _

INVERT3: (int, int) = = Unit


The This function object has a apply method (inherited from Function2[t1, T2, R]) with the following signature:


def apply (V1:T1, v2:t2): R


In particular, this method defines the no default values for its arguments! As a consequence, invert3 (v1 = 2) leads to a compile time error (as there is not enough actual arguments for method apply ).


The argument names V1 and V2 are the names as they is defined in the Apply-method of Function2[t1, T2, R]. The argument names of Apply-method of every function with both arguments is named V1 and V2, in particular these names is By no. means related to the argument names of method Invert3. Method Invert3 have by chance arguments with the same names, but unfortunately in a different order. Invert3 (v1 = 2, v2 = 1) Prints 1, 2, 3 as the parameter V1 (which corresponds to the parameter V2 in method invert) is set to 2 and parameter V2 (which corresponds to the parameter V1 in method invert) are set to 1.


The effect of the 18 partial function on the default parameters of the Scala fun topic

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.