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

Previous

\n", "

Next

\n", "

Tour of Scala

\n", "
\n", "\n", "# Default Parameter Values\n", "\n", "Scala provides the ability to give parameters default values that can be used to allow a caller to omit those parameters." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: System starting\n", "WARNING: User not found\n" ] }, { "data": { "text/plain": [ "defined \u001b[32mfunction\u001b[39m \u001b[36mlog\u001b[39m" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def log(message: String, level: String = \"INFO\") = println(s\"$level: $message\")\n", "\n", "log(\"System starting\") // prints INFO: System starting\n", "log(\"User not found\", \"WARNING\") // prints WARNING: User not found" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameter `level` has a default value so it is optional. On the last line, the argument `\"WARNING\"` overrides the default argument `\"INFO\"`. Where you might do overloaded methods in Java, you can use methods with optional parameters to achieve the same effect. However, if the caller omits an argument, any following arguments must be named." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m\n", "\u001b[36mpoint1\u001b[39m: \u001b[32mPoint\u001b[39m = ammonite.$sess.cmd1$Helper$Point@52fcf939" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class Point(val x: Double = 0, val y: Double = 0)\n", "\n", "val point1 = new Point(y = 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we have to say `y = 1`.\n", "\n", "Note that default parameters in Scala are not optional when called from Java code:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "attributes": { "classes": [ "tut" ], "id": "" } }, "outputs": [ { "data": { "text/plain": [ "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "// Point.scala\n", "class Point(val x: Double = 0, val y: Double = 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```java\n", "// Main.java\n", "public class Main {\n", " public static void main(String[] args) {\n", " Point point = new Point(1); // does not compile\n", " }\n", "}\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

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 }