{"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":{"footnotes":""},"categories":[8],"tags":[23],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>InstanceOf &quot;Smart Casts&quot; with Java 8 Lambdas - Benji&#039;s Blog<\/title>\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=\"author\" content=\"benji\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"benji\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/\"},\"author\":{\"name\":\"benji\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"headline\":\"InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas\",\"datePublished\":\"2013-12-04T11:37:22+00:00\",\"dateModified\":\"2013-12-04T11:47:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/\"},\"wordCount\":94,\"commentCount\":0,\"keywords\":[\"Java\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/\",\"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\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/2013\\\/12\\\/04\\\/instanceof-smart-casts-with-java-8-lambdas\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas\"}]},{\"@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\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/#\\\/schema\\\/person\\\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g\",\"caption\":\"benji\"},\"sameAs\":[\"http:\\\/\\\/benjiweber.co.uk\"],\"url\":\"https:\\\/\\\/benjiweber.co.uk\\\/blog\\\/author\\\/benji\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"InstanceOf \"Smart Casts\" with Java 8 Lambdas - Benji&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/","og_locale":"en_US","og_type":"article","og_title":"InstanceOf \"Smart Casts\" with Java 8 Lambdas - Benji&#039;s Blog","og_description":"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;","og_url":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/","og_site_name":"Benji&#039;s Blog","article_published_time":"2013-12-04T11:37:22+00:00","article_modified_time":"2013-12-04T11:47:42+00:00","author":"benji","twitter_card":"summary_large_image","twitter_misc":{"Written by":"benji","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/#article","isPartOf":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/"},"author":{"name":"benji","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09"},"headline":"InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas","datePublished":"2013-12-04T11:37:22+00:00","dateModified":"2013-12-04T11:47:42+00:00","mainEntityOfPage":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/"},"wordCount":94,"commentCount":0,"keywords":["Java"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/","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"},"breadcrumb":{"@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/benjiweber.co.uk\/blog\/2013\/12\/04\/instanceof-smart-casts-with-java-8-lambdas\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/benjiweber.co.uk\/blog\/"},{"@type":"ListItem","position":2,"name":"InstanceOf &#8220;Smart Casts&#8221; with Java 8 Lambdas"}]},{"@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":{"@type":"EntryPoint","urlTemplate":"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09","name":"benji","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/417ada828fa4be09324820d24c0d93c3bfcbae183629e09117c9f4c4ab7ed43a?s=96&d=mm&r=g","caption":"benji"},"sameAs":["http:\/\/benjiweber.co.uk"],"url":"https:\/\/benjiweber.co.uk\/blog\/author\/benji\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"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}]}}