{"id":3459,"date":"2016-03-17T00:55:19","date_gmt":"2016-03-17T00:55:19","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3459"},"modified":"2016-03-17T00:55:19","modified_gmt":"2016-03-17T00:55:19","slug":"cast-class-scala","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/cast-class-scala\/","title":{"rendered":"Cast a class in scala"},"content":{"rendered":"<p>The following example shows a way to cast a value in Scala. In this example, one of the java utility methods called &#8220;getNetworkInterfaces&#8221; class returns something we want cast to a more specific class, a NetworkInterface:<\/p>\n<pre lang=\"scala\">\nval e = NetworkInterface.getNetworkInterfaces\nwhile(e.hasMoreElements) {\n  val networkInterface = e.nextElement match {\n    case e: NetworkInterface => e\n    case _ => ???\n  }\n\n  \/\/ process this network adapter\n}   \n<\/pre>\n<p>Once the &#8220;match&#8221; evaluates, the &#8220;networkInterface&#8221; variable will have the desired type.<\/p>\n<p>If the cast fails, we throw NotImplementedException (??? is a shortcut for this). You could alternately try different casts by matching on other patterns.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following example shows a way to cast a value in Scala. In this example, one of the java utility methods called &#8220;getNetworkInterfaces&#8221; class returns something we want cast to a more specific class, a NetworkInterface: val e = NetworkInterface.getNetworkInterfaces while(e.hasMoreElements) { val networkInterface = e.nextElement match { case e: NetworkInterface => e case _ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/cast-class-scala\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cast a class in scala&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[480,485],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3459"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=3459"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3459\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}