{"id":8364,"date":"2021-04-23T20:54:05","date_gmt":"2021-04-23T20:54:05","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=8364"},"modified":"2024-04-13T06:12:25","modified_gmt":"2024-04-13T06:12:25","slug":"scala-apply-method","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/","title":{"rendered":"Scala &#8211; Apply Method"},"content":{"rendered":"\r\n<h2><span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;What is the apply function in Scala?&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;}\">What is the apply function in Scala<\/span><\/h2>\r\n<p>In Scala, the <code>apply<\/code> function is a special method defined in companion objects of classes or in standalone objects. The primary purpose of the <code>apply<\/code> function is to provide a convenient way to create new instances of a class or invoke an object as if it were a function.<\/p>\r\n<p>When you define an <code>apply<\/code> method in a companion object, you can use it to construct instances of the corresponding class without explicitly calling the <code>new<\/code> keyword. This allows for a more concise and readable syntax when creating instances. For example:<\/p>\r\n<p>In this example, the <code>apply<\/code> method in the <code>Person<\/code> companion object enables us to create a new <code>Person<\/code> instance simply by calling <code>Person(\"Alice\", 30)<\/code>, which internally delegates to <code>Person.apply(\"Alice\", 30)<\/code>.<\/p>\r\n<p>In Scala the\u00a0<strong>apply()<\/strong>\u00a0method is <a href=\"https:\/\/prwatech.in\/blog\/#\">used<\/a> to select an <a href=\"http:\/\/scala-lang.org\">element<\/a> present in the list by its index position.<\/p>\r\n\r\n\r\n\r\n<p><strong>use case 1:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>scala&gt; object prwa{<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 def apply(name: String): String={<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 &#8220;Hello %s&#8221;.format(name)<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<p>object prwa<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; prwa.apply(&#8220;prava&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>1scala&gt; prwa(&#8220;binoj&#8221;)<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"209\" class=\"wp-image-8365\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png 415w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56-300x151.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>use case 2:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>scala&gt; class Prwatech\u00a0 {<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 private val elements = Array(&#8220;spark&#8221;,&#8221;scala&#8221;,&#8221;mysql&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 def apply(index:Int) = if(index&lt;elements.length) elements(index) else &#8220;No element found&#8221;<\/p>\r\n\r\n\r\n\r\n<p>\u00a0 }<\/p>\r\n\r\n\r\n\r\n<p>class Prwatech<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; val Prwatech = new Prwatech<\/p>\r\n\r\n\r\n\r\n<p>2scala&gt; println(Prwatech(1))<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; println(Prwatech(0))<\/p>\r\n\r\n\r\n\r\n<p>4scala&gt; println(Prwatech(2))<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; println(Prwatech(3))<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"261\" class=\"wp-image-8366\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-57.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-57.png 606w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-57-300x129.png 300w\" sizes=\"auto, (max-width: 606px) 100vw, 606px\" \/><\/figure>\r\n\r\n\r\n\r\n<p><strong>use case 3:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>Scala&gt; object prwatech{<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 def apply(name: String): String={<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 &#8220;My Name is %s&#8221;.format(name)<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<p>\u00a0\u00a0\u00a0\u00a0 }<\/p>\r\n\r\n\r\n\r\n<p>object prwatech<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; prwatech.apply(&#8220;Mark&#8221;)<\/p>\r\n\r\n\r\n\r\n<p>scala&gt; prwatech.apply(&#8220;Steve&#8221;)<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"209\" class=\"wp-image-8367\" src=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-58.png\" alt=\"\" srcset=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-58.png 399w, https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-58-300x157.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/figure>\r\n<p>&nbsp;<\/p>\r\n<p>What is the apply function in Scala<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>What is the apply function in Scala In Scala, the apply function is a special method defined in companion objects of classes or in standalone objects. The primary purpose of the apply function is to provide a convenient way to create new instances of a class or invoke an object as if it were a [&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":[1081,1087,1079,1086,1082,1085,1078,1080,1083,1084],"class_list":["post-8364","post","type-post","status-publish","format-standard","hentry","category-scala","category-scala-modules-scala","tag-apply-method","tag-apply-method-concept-in-scala","tag-apply-method-in-scala","tag-apply-method-scala-use-cases","tag-apply-method-use-cases","tag-apply-method-use-cases-in-scala","tag-apply-methods","tag-scala-apply-method","tag-scala-apply-method-example","tag-scala-apply-method-example-program"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is the apply function in Scala - Prwatech<\/title>\n<meta name=\"description\" content=\"Master What is the apply function in Scala - 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=\"What is the apply function in Scala - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Master What is the apply function in Scala - Dive deep with our expert instructors and comprehensive curriculum.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/\" \/>\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-04-23T20:54:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-13T06:12:25+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png\" \/>\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-apply-method\/\",\"url\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/\",\"name\":\"What is the apply function in Scala - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png\",\"datePublished\":\"2021-04-23T20:54:05+00:00\",\"dateModified\":\"2024-04-13T06:12:25+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Master What is the apply function in Scala - Dive deep with our expert instructors and comprehensive curriculum.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage\",\"url\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png\",\"contentUrl\":\"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png\",\"width\":415,\"height\":209},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Scala &#8211; Apply Method\"}]},{\"@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":"What is the apply function in Scala - Prwatech","description":"Master What is the apply function in Scala - 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":"What is the apply function in Scala - Prwatech","og_description":"Master What is the apply function in Scala - Dive deep with our expert instructors and comprehensive curriculum.","og_url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2021-04-23T20:54:05+00:00","article_modified_time":"2024-04-13T06:12:25+00:00","og_image":[{"url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png","type":"","width":"","height":""}],"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-apply-method\/","url":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/","name":"What is the apply function in Scala - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage"},"image":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage"},"thumbnailUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png","datePublished":"2021-04-23T20:54:05+00:00","dateModified":"2024-04-13T06:12:25+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Master What is the apply function in Scala - Dive deep with our expert instructors and comprehensive curriculum.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#primaryimage","url":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png","contentUrl":"https:\/\/prwatech.in\/blog\/wp-content\/uploads\/2021\/04\/image-56.png","width":415,"height":209},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/scala\/scala-modules-scala\/scala-apply-method\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Scala &#8211; Apply Method"}]},{"@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\/8364","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=8364"}],"version-history":[{"count":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8364\/revisions"}],"predecessor-version":[{"id":11416,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/8364\/revisions\/11416"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=8364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=8364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=8364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}