{"id":299,"date":"2013-12-04T12:37:22","date_gmt":"2013-12-04T11:37:22","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=299"},"modified":"2013-12-04T12:47:42","modified_gmt":"2013-12-04T11:47:42","slug":"instanceof-smart-casts-with-java-8-lambdas","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/","title":{"rendered":"InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas"},"content":{"rendered":"<p class=\"lead\">As with try-as-expression, there are many other language features we can simulate with Lambdas.<\/p>\n<p>Another example is removing the cast commonly needed with instanceof. Kotlin has a nice feature called <a href=\"http:\/\/confluence.jetbrains.com\/display\/Kotlin\/Type+casts\">smart casts<\/a>, which allows you to do<\/p>\n<pre lang=\"java\">\r\n    if (x instanceof Duck) {\r\n        x.quack();\r\n    }\r\n<\/pre>\n<p>Where x is &#8220;casted&#8221; to Duck within the block.<\/p>\n<p>I <a href=\"http:\/\/benjiweber.co.uk\/blog\/2010\/09\/16\/java-abuse-inline-instanceof\/\">previously blogged<\/a> how to simulate this in older Java using dynamic proxies.<\/p>\n<p>Now in Java 8 you can do it in a slightly less terrible way, and also make it an expression at the same time.<\/p>\n<pre lang=\"java\">\r\n    @Test\r\n    public void should_return_value_when_input_object_is_instance() {\r\n        Object foo = \"ffoo\";\r\n\r\n        String result = when(foo).instanceOf(String.class)\r\n                .then(s -> s.substring(1))\r\n                .otherwise(\"incorrect\");\r\n\r\n        assertEquals(\"foo\", result);\r\n    }\r\n<\/pre>\n<p><a href=\"https:\/\/github.com\/benjiman\/expressions\/blob\/master\/src\/test\/java\/uk\/co\/benjiweber\/expressions\/InstanceOfTest.java\">More examples<\/a> and <a href=\"https:\/\/github.com\/benjiman\/expressions\/blob\/master\/src\/main\/java\/uk\/co\/benjiweber\/expressions\/InstanceOf.java\">implementation<\/a> on github.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As with try-as-expression, there are many other language features we can simulate with Lambdas. Another example is removing the cast commonly needed with instanceof. Kotlin has a nice feature called smart casts, which allows you to do if (x instanceof Duck) { x.quack(); } Where x is &#8220;casted&#8221; to Duck within the block. I previously&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/\" class=\"more-link\" title=\"Read InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[23],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"InstanceOf &quot;Smart Casts&quot; with Java 8 Lambdas - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"As with try-as-expression, there are many other language features we can simulate with Lambdas. Another example is removing the cast commonly needed with instanceof. Kotlin has a nice feature called smart casts, which allows you to do if (x instanceof Duck) { x.quack(); } Where x is &#8220;casted&#8221; to Duck within the block. I previously... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2013-12-04T11:37:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-12-04T11:47:42+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/\",\"name\":\"InstanceOf \\\"Smart Casts\\\" with Java 8 Lambdas - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"datePublished\":\"2013-12-04T11:37:22+00:00\",\"dateModified\":\"2013-12-04T11:47:42+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/299"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":7,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/299\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}