Scala Fun Question 6

Source: Internet
Author: User

This must be a place for the Scala compiler to improve. A person can infer it immediately, and the compiler should be able to infer the

package pz005object pz005 {   def square[t: numeric] (n: t)  = implicitly[numeric[t]].times (n, n)   def twicea[t] (f: t => t, a: t)  = f (f (a))  twiceA ( SQUARE, 2)  //  error not found implicit value   def twiceb[t] (f: t => t) (a: t)  = f (f (a))   twiceb (square) (2)  //  error ibid.   def twicec[t] (a: t,  F: T => T)  = f (f (a))   TWICEC (2, square)  //error Ibid    def twiced[t] (a: t) (f: t => t)  = f (f (a))   twiced (2) ( Square)   // ok,  The compiler detects that 2 is the parameter of type T to be passed to square} 

Square requires an implicit parameter of type numeric[t]

To determine whether the current context has such an implied parameter, you must first identify the type of T

Only the 4th method, "Enough ahead of time" to tell the compiler

Scala Fun topic 6

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.