{"id":2051,"date":"2017-02-08T12:07:18","date_gmt":"2017-02-08T12:07:18","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2051"},"modified":"2017-02-08T12:09:52","modified_gmt":"2017-02-08T12:09:52","slug":"groovy-few-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/groovy-few-basics\/","title":{"rendered":"Groovy Few Basics"},"content":{"rendered":"<p><strong>What <em>def<\/em> Keyword Does?<\/strong><br \/>\ndef is a replacement for a type name. In variable definitions it is used to indicate that you don\u2019t care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use &#8220;def&#8221; in replacement. This is needed to make variable definitions detectable for the Groovy parser.You can think of def as an alias of Object and you will understand it in an instant.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndef name = &quot;Stephanie&quot;\r\nprintln name.toUpperCase() \/\/ no cast required\r\n\r\n<\/pre>\n<p>while you would need an explicit cast in the Java version<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nObject name = &quot;Stephanie&quot;;\r\nSystem.out.println(((String) name).toUpperCase());\r\n<\/pre>\n<p>Omitting the &#8220;def&#8221; keyword puts the variable in the bindings for the current script and groovy treats it (mostly) like a globally scoped variable:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nx = 1\r\nassert x == 1\r\nassert this.binding.getVariable(&quot;x&quot;) == 1\r\n\r\n<\/pre>\n<p>Using the def keyword instead does not put the variable in the scripts bindings:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ndef y = 2\r\nassert y == 2\r\ntry {\r\n    this.binding.getVariable(&quot;y&quot;) \r\n} catch (groovy.lang.MissingPropertyException e) {\r\n    println &quot;error caught&quot;\r\n} \r\n\r\n<\/pre>\n<blockquote><p>Prints: &#8220;error caught&#8221;<\/p><\/blockquote>\n<p>If you define a method in your script, it won&#8217;t have access to the variables that are created with &#8220;def&#8221; in the body of the main script as they aren&#8217;t in scope:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">x = 1\r\ndef y = 2\r\n\r\npublic bar() {\r\n    assert x == 1\r\n\r\n    try {\r\n        assert y == 2\r\n    } catch (groovy.lang.MissingPropertyException e) {\r\n        println &quot;error caught&quot;\r\n    }\r\n}\r\n\r\nbar()\r\n<\/pre>\n<blockquote><p>prints &#8220;error caught&#8221;<\/p><\/blockquote>\n<p>The &#8220;y&#8221; variable isn&#8217;t in scope inside the function. &#8220;x&#8221; is in scope as groovy will check the bindings of the current script for the variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What def Keyword Does? def is a replacement for a type name. In variable definitions it is used to indicate that you don\u2019t care about the type. In variable definitions it is mandatory to either provide a type name explicitly or to use &#8220;def&#8221; in replacement. This is needed to make variable definitions detectable for&hellip; <a href=\"https:\/\/codethataint.com\/blog\/groovy-few-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[222],"tags":[],"class_list":["post-2051","post","type-post","status-publish","format-standard","hentry","category-groovy"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2051"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2051\/revisions"}],"predecessor-version":[{"id":2056,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2051\/revisions\/2056"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}