Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 21: Implicit Conversions and Par... > 21.2 Rules for implicits - Pg. 446

C HAPTER 21 · Implicit Conversions and Parameters This section has shown you some of the power of implicit conversions, and how they let you dress up existing libraries. In the next sections you'll learn the rules that determine when implicit conversions are tried and how they are found. 21.2 Rules for implicits Implicit definitions are those that the compiler is allowed to insert into a program in order to fix any of its type errors. For example, if x + y does not type check, then the compiler might change it to convert(x) + y , where convert is some available implicit conversion. If convert changes x into something that has a + method, then this change might fix a program so that it type checks and runs correctly. If convert really is just a simple conversion function, then leaving it out of the source code can be a clarification. Implicit conversions are governed by the following general rules: Marking Rule: Only definitions marked implicit are available. The implicit keyword is used to mark which declarations the compiler may use as implicits. You can use it to mark any variable, function, or object