Scala study notes 20-method return type inference

Source: Internet
Author: User

In addition to the type of push evolution, Scala also deduces the return type of the method. But here's one point to note: The deduction of the method return type relies on how the method is defined. If you define a method with an equal sign "=", Scala will deduce the method return type, otherwise it will assume that the return of the method is void. See an example:

def printmethodinfo (methodname:string) {  println ("The return type of" + MethodName + "is"      + getclass ( ). Getdeclaredmethod (MethodName6= {6= 6= 6printmethodinfo ("Method1" ) Printmethodinfo ("Method2") printmethodinfo ("method3")printmethodinfo ("METHOD4")

One method is defined in the customary way, and the second is defined using the equal sign. The two methods are simple, and the only difference is whether you use the equals sign when you define it. This difference determines whether Scala will deduce the return type of the method. Take a look at the results of the above code:

The code execution results are consistent with expectations (ignore warnings from the first few lines).

If the method body is very simple and can be written as an expression, then the curly braces around the body of the method can be removed, just like the method three. Also mentioned earlier, if the method has no parameters, the parentheses can be omitted, which is how method four is defined.

In addition, you can explicitly declare types. This declares the return value of method four as a double. Although the return value of method four is actually an int value, the return type of method four can also be declared as unit, short, long, float, and so on as long as the return value is compatible with the declared return type. If the declared return type is incompatible with the actual return value, Scala will report a compile-time error with a type mismatch.

Method return type inference is also associated with the previous usage of return: If the return keyword is not written, the method call automatically returns the last evaluated expression.

In general, using =, the type of Scala deduction method is better. This can be less worrying about one thing, let's build a good type deduction for our service.

#######

Scala study notes 20-method return type inference

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.