{"id":4764,"date":"2017-03-26T19:49:53","date_gmt":"2017-03-26T19:49:53","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4764"},"modified":"2017-03-26T19:49:53","modified_gmt":"2017-03-26T19:49:53","slug":"how-to-create-property-like-functionality-in-scala","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/how-to-create-property-like-functionality-in-scala\/","title":{"rendered":"How to create property like functionality in Scala"},"content":{"rendered":"<p>Scala has a slightly strange syntax for the equivalent of C# properties. Let&#8217;s take a look<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nclass Person(private var _name: String) {\r\n  \/\/ getter\r\n  def name = _name\r\n  \/\/ setter\r\n  def name_=(m: Int): Unit = {\r\n    println(&quot;Setter called&quot;)\r\n    _name = m\r\n  }\r\n}\r\n\r\n\/\/ code to call the above\r\nval c = new Person(&quot;Scooby Doo&quot;)\r\nc.name = &quot;Scrappy Doo&quot;\r\n<\/pre>\n<p>In the Person constructor we mark the <em>_name<\/em> as private so it cannot be altered outside of the class and notice it&#8217;s marked as a var as its used as the backing field for the <em>name<\/em> property. The getter <em>name<\/em> is pretty standard but the <em>name_=<\/em> is a little stranger. <\/p>\n<p><em>Note: There can be no space between the _ and =<\/em><\/p>\n<p>This is the syntax for a setter, you do not need to use something like <em>c.name_= &#8220;Name&#8221;<\/em> it simply translates to <em>c.Name = &#8220;Name&#8221;<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Scala has a slightly strange syntax for the equivalent of C# properties. Let&#8217;s take a look class Person(private var _name: String) { \/\/ getter def name = _name \/\/ setter def name_=(m: Int): Unit = { println(&quot;Setter called&quot;) _name = m } } \/\/ code to call the above val c = new Person(&quot;Scooby Doo&quot;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[165],"tags":[],"class_list":["post-4764","post","type-post","status-publish","format-standard","hentry","category-scala"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4764"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4764\/revisions"}],"predecessor-version":[{"id":4783,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4764\/revisions\/4783"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}