{"id":8616,"date":"2021-05-22T15:27:19","date_gmt":"2021-05-22T15:27:19","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8616"},"modified":"2024-04-13T10:45:34","modified_gmt":"2024-04-13T10:45:34","slug":"scala-options-scala-option-a-gentle-introduction","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/","title":{"rendered":"Scala &#8211; options"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Scala Option: A Gentle Introduction&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:6145,&quot;3&quot;:{&quot;1&quot;:0,&quot;3&quot;:1},&quot;14&quot;:{&quot;1&quot;:3,&quot;3&quot;:1},&quot;15&quot;:&quot;Arial&quot;}\">Scala Option: A Gentle Introduction<\/span><\/h2>\r\n<p>&nbsp;<\/p>\r\n<p>Scala Option[ T ] is a <a href=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-high-order-functions\/\">compartment<\/a> for nothing or one <a href=\"http:\/\/scala-lang.org\">component<\/a> of a given kind. An Option[T] can be either Some[T] or No object, which addresses a missing worth. For example, the get technique for Scala&#8217;s Map produces Some(value) if a value comparing to a given key has been or None if the given key isn&#8217;t mention in the Map.<\/p>\r\n<p>Scala&#8217;s <code>Option<\/code> type is a fundamental concept in functional programming that addresses the challenge of representing absence of a value in a type-safe manner. <code>Option<\/code> is used to encapsulate an optional value that may or may not exist, providing a safer alternative to <code>null<\/code> references commonly used in other languages.<\/p>\r\n<p>The <code>Option<\/code> type in Scala is defined as a generic container that can hold either a <code>Some<\/code> value, representing the presence of a value, or <code>None<\/code>, representing the absence of a value. This approach eliminates the risk of null pointer exceptions and encourages safer and more predictable code.<\/p>\r\n\r\n\r\n\r\n<h4><strong>Use case 1:<\/strong><\/h4>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\n   \/\/ Main method\r\n    def main(args: Array[String])\r\n    {\r\n  \r\n        \/\/ Creating a Map\r\n        val name = Map(\"Abhi\" -&gt; \"author\", \r\n                        \"Prabha\" -&gt; \"coder\")\r\n  \r\n        \/\/ Accessing keys of the map\r\n        val x = name.get(\"Abhi\")\r\n        val y = name.get(\"Sandeep\")\r\n  \r\n        \/\/ Displays Some if the key is\r\n        \/\/ found else None \r\n        println(x)\r\n        println(y)\r\n    }\r\n}\t\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Output:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Some(author)\r\nNone\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Here, key of the value\u00a0<em>Abhi<\/em>\u00a0 so, <em>Some<\/em> is return for it but key of the value <em>Sandeep<\/em>\u00a0so, <em>None<\/em> is return for it.<\/p>\r\n\r\n\r\n\r\n<p><strong>Use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>Using Pattern Matching:\r\nobject prwatech {\r\n   \/\/ Main method\r\n    def main(args: Array[String])\r\n    {\r\n  \r\n        \/\/ Creating a Map\r\n        val name = Map(\"Abhi\" -&gt; \"author\", \r\n                        \"Prabha\" -&gt; \"coder\")\r\n  \r\n        \/\/Accessing keys of the map\r\n        println(patrn(name.get(\"Abhi\")))\r\n        println(patrn(name.get(\"Rahul\")))\r\n    }\r\n  \r\n    \/\/ Using Option with Pattern \r\n    \/\/ matching\r\n    def patrn(z: Option[String]) = z match \r\n    {\r\n  \r\n        \/\/ for 'Some' class the key for\r\n        \/\/ the given value is displayed\r\n        case Some(s) =&gt; (s)\r\n  \r\n        \/\/ for 'None' class the below string \r\n        \/\/ is displayed\r\n        case None =&gt; (\"key not found\")\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Output:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>author\r\nkey not found\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<p><strong>getOrElse() Method:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>This technique returns either a value in the event that is available or a default value when its not available. Here, For Some class a value is return and for None class a default value returns.Scala Option: A Gentle Introduction<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>object prwatech {\r\n   \/\/ Main method\r\n    def main(args: Array[String])\r\n    {\r\n  \r\n        \/\/ Using Some class\r\n        val some:Option[Int] = Some(5)\r\n  \r\n        \/\/ Using None class\r\n        val none:Option[Int] = None \r\n  \r\n        \/\/ Applying getOrElse method\r\n        val x = some.getOrElse(0)\r\n        val y = none.getOrElse(1)\r\n  \r\n        \/\/ Displays the key in the \r\n        \/\/ class Some\r\n        println(x)\r\n  \r\n        \/\/ Displays default value \r\n        println(y)\r\n    }\r\n}\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Output<\/strong>:<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>5\r\n1<\/code><\/pre>\r\n","protected":false},"excerpt":{"rendered":"<p>Scala Option: A Gentle Introduction &nbsp; Scala Option[ T ] is a compartment for nothing or one component of a given kind. An Option[T] can be either Some[T] or No object, which addresses a missing worth. For example, the get technique for Scala&#8217;s Map produces Some(value) if a value comparing to a given key has [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[565,1698],"tags":[1338,1340,1335,1341,1339,1336,1334,1343,1337],"class_list":["post-8616","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-how-option-is-used-in-scala","tag-option-function-in-scala","tag-option-in-scala","tag-option-method-scala","tag-options-use-cases-in-scala","tag-options-uses-in-scala","tag-scala-options","tag-scala-options-explanation","tag-what-is-option-in-scala"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Scala Option: A Gentle Introduction - prwatech<\/title>\n<meta name=\"description\" content=\"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scala Option: A Gentle Introduction - prwatech\" \/>\n<meta property=\"og:description\" content=\"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-22T15:27:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T10:45:34+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/\",\"name\":\"Scala Option: A Gentle Introduction - prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2021-05-22T15:27:19+00:00\",\"dateModified\":\"2024-04-13T10:45:34+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; options\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scala Option: A Gentle Introduction - prwatech","description":"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Scala Option: A Gentle Introduction - prwatech","og_description":"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-05-22T15:27:19+00:00","article_modified_time":"2024-04-13T10:45:34+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/","name":"Scala Option: A Gentle Introduction - prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2021-05-22T15:27:19+00:00","dateModified":"2024-04-13T10:45:34+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master Scala Option: A Gentle Introduction - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-options-scala-option-a-gentle-introduction\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; options"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=8616"}],"version-history":[{"count":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8616\/revisions"}],"predecessor-version":[{"id":11455,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8616\/revisions\/11455"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}