{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
\n", "\n", "# Implicit Conversions\n", "\n", "An implicit conversion from type `S` to type `T` is defined by an implicit value which has function type `S => T`, or by an implicit method convertible to a value of that type.\n", "\n", "Implicit conversions are applied in two situations:\n", "\n", "* If an expression `e` is of type `S`, and `S` does not conform to the expression's expected type `T`.\n", "* In a selection `e.m` with `e` of type `S`, if the selector `m` does not denote a member of `S`.\n", "\n", "In the first case, a conversion `c` is searched for which is applicable to `e` and whose result type conforms to `T`.\n", "In the second case, a conversion `c` is searched for which is applicable to `e` and whose result contains a member named `m`.\n", "\n", "If an implicit method `List[A] => Ordered[List[A]]` is in scope, as well as an implicit method `Int => Ordered[Int]`, the following operation on the two lists of type `List[Int]` is legal:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```scala\n", "List(1, 2, 3) <= List(4, 5)\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An implicit method `Int => Ordered[Int]` is provided automatically through `scala.Predef.intWrapper`. An example of an implicit method `List[A] => Ordered[List[A]]` is provided below." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "\u001b[32mimport \u001b[39m\u001b[36mscala.language.implicitConversions\n", "\n", "\u001b[39m\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mlist2ordered\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import scala.language.implicitConversions\n", "\n", "implicit def list2ordered[A](x: List[A])\n", " (implicit elem2ordered: A => Ordered[A]): Ordered[List[A]] =\n", " new Ordered[List[A]] { \n", " //replace with a more useful implementation\n", " def compare(that: List[A]): Int = 1\n", " }" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The implicitly imported object `scala.Predef` declares several aliases to frequently used types (e.g. `scala.collection.immutable.Map` is aliased to `Map`) and methods (e.g. `assert`) but also several implicit conversions.\n", "\n", "For example, when calling a Java method that expects a `java.lang.Integer`, you are free to pass it a `scala.Int` instead. That's because Predef includes the following implicit conversions:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "\u001b[32mimport \u001b[39m\u001b[36mscala.language.implicitConversions\n", "\n", "\u001b[39m\n", "defined \u001b[32mfunction\u001b[39m \u001b[36mint2Integer\u001b[39m" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import scala.language.implicitConversions\n", "\n", "implicit def int2Integer(x: Int) =\n", " java.lang.Integer.valueOf(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because implicit conversions can have pitfalls if used indiscriminately the compiler warns when compiling the implicit conversion definition.\n", "\n", "To turn off the warnings take either of these actions:\n", "\n", "* Import `scala.language.implicitConversions` into the scope of the implicit conversion definition\n", "* Invoke the compiler with `-language:implicitConversions`\n", "\n", "No warning is emitted when the conversion is applied by the compiler.\n", "

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
" ] } ], "metadata": { "kernelspec": { "display_name": "Scala (2.13)", "language": "scala", "name": "scala213" }, "language_info": { "codemirror_mode": "text/x-scala", "file_extension": ".scala", "mimetype": "text/x-scala", "name": "scala", "nbconvert_exporter": "script", "version": "2.13.1" } }, "nbformat": 4, "nbformat_minor": 4 }