/* * Copyright (C) 2007-2008 Artima, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Automatically generated Scala interpreter transcript from: * * Programming in Scala (First Edition, Version 6) * by Martin Odersky, Lex Spoon, Bill Venners * * http://booksites.artima.com/programming_in_scala */ scala> val i: Int = 3.5 :5: error: type mismatch; found : Double(3.5) required: Int val i: Int = 3.5 ^ scala> implicit def doubleToInt(x: Double) = x.toInt doubleToInt: (x: Double)Int scala> val i: Int = 3.5 i: Int = 3 scala> class Rational(n: Int, d: Int) { def this(n: Int) = this(n, 1) private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) private val g = gcd(n, d) val numer: Int = n / g val denom: Int = d / g def +(that: Rational): Rational = new Rational(numer * that.denom + that.numer * denom, denom * that.denom) def +(that: Int): Rational = this + new Rational(that) override def toString() = numer+"/"+denom } defined class Rational scala> val oneHalf = new Rational(1, 2) oneHalf: Rational = 1/2 scala> oneHalf + oneHalf res0: Rational = 1/1 scala> oneHalf + 1 res1: Rational = 3/2 scala> implicit def intToRational(x: Int) = new Rational(x, 1) intToRational: (x: Int)Rational scala> 1 + oneHalf res2: Rational = 3/2 scala> class PreferredPrompt(val preference: String) defined class PreferredPrompt scala> object Greeter { def greet(name: String)(implicit prompt: PreferredPrompt) { println("Welcome, "+ name +". The system is ready.") println(prompt.preference) } } defined module Greeter scala> val bobsPrompt = new PreferredPrompt("relax> ") bobsPrompt: PreferredPrompt = PreferredPrompt@31ccbefe scala> Greeter.greet("Bob")(bobsPrompt) Welcome, Bob. The system is ready. relax> scala> object JoesPrefs { implicit val prompt = new PreferredPrompt("Yes, master> ") } defined module JoesPrefs scala> Greeter.greet("Joe") :11: error: could not find implicit value for parameter prompt: PreferredPrompt Greeter.greet("Joe") ^ scala> import JoesPrefs._ import JoesPrefs._ scala> Greeter.greet("Joe") Welcome, Joe. The system is ready. Yes, master> scala> class PreferredPrompt(val preference: String) class PreferredDrink(val preference: String) object Greeter { def greet(name: String)(implicit prompt: PreferredPrompt, drink: PreferredDrink) { println("Welcome, "+ name +". The system is ready.") print("But while you work, ") println("why not enjoy a cup of "+ drink.preference +"?") println(prompt.preference) } } object JoesPrefs { implicit val prompt = new PreferredPrompt("Yes, master> ") implicit val drink = new PreferredDrink("tea") } defined class PreferredPrompt defined class PreferredDrink defined module Greeter defined module JoesPrefs scala> Greeter.greet("Joe") :17: error: could not find implicit value for parameter prompt: PreferredPrompt Greeter.greet("Joe") ^ scala> import JoesPrefs._ import JoesPrefs._ scala> Greeter.greet("Joe")(prompt, drink) Welcome, Joe. The system is ready. But while you work, why not enjoy a cup of tea? Yes, master> scala> Greeter.greet("Joe") Welcome, Joe. The system is ready. But while you work, why not enjoy a cup of tea? Yes, master> scala> def maxListImpParm[T](elements: List[T]) (implicit orderer: T => Ordered[T]): T = elements match { case List() => throw new IllegalArgumentException("empty list!") case List(x) => x case x :: rest => val maxRest = maxListImpParm(rest)(orderer) if (orderer(x) > maxRest) x else maxRest } maxListImpParm: [T](elements: List[T])(implicit orderer: (T) => Ordered[T])T scala> maxListImpParm(List(1,5,10,3)) res9: Int = 10 scala> maxListImpParm(List(1.5, 5.2, 10.7, 3.14159)) res10: Double = 10.7 scala> maxListImpParm(List("one", "two", "three")) res11: java.lang.String = two scala> def printLength(seq: Seq[Int]) = println(seq.length) printLength: (seq: Seq[Int])Unit scala> implicit def intToRange(i: Int) = 1 to i intToRange: (i: Int)scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne scala> implicit def intToDigits(i: Int) = i.toString.toList.map(_.toInt) intToDigits: (i: Int)List[Int] scala> printLength(12) :24: error: type mismatch; found : Int(12) required: Seq[Int] Note that implicit conversions are not applicable because they are ambiguous: both method intToDigits in object $iw of type (i: Int)List[Int] and method intToRange in object $iw of type (i: Int)scala.collection.immutable.Range.Inclusive with scala.collection.immutable.Range.ByOne are possible conversion functions from Int(12) to Seq[Int] printLength(12) ^ scala> val chars: List[Char] = "xyz" :22: error: type mismatch; found : java.lang.String("xyz") required: List[Char] val chars: List[Char] = "xyz" ^ scala> val chars: List[Char] = wrapString("xyz") :22: error: type mismatch; found : scala.collection.immutable.WrappedString required: List[Char] val chars: List[Char] = wrapString("xyz") ^