Scala puzzle 22 Function Rewrite, positional parameter, parameter name

Source: Internet
Author: User


Class C {def sum (x:int = 1, Y:int = 2): Int = x + y} class D extends C {override def sum (y:int = 3, X:int = 4): Int = Super.sum (x, y)} val d:d = new D val c:c = d c.sum (x = 0) d.sum (x = 0)



Explanation


Scala uses the static type of a variable to bind parameter names, but the defaults is determined by the runtime type:


The binding of parameter names is do by the compiler and the only information the compiler can use is the static type of The variable.

For parameters with a default value the compiler creates methods which compute the default argument expressions (sls§4.6) . In the above example, both classes C and D contain the methods sum$default$1 and sum$default$2 for the default Paramet ERs. When a parameter was missing, the compiler uses the result of these methods, and these methods are invoked at Run-time.


Scala puzzle 22 Function Rewrite, positional parameter, parameter name

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.