Groovy default params to avoid one-argument methods being called without params

Recently, I saw an interesting tweet, mentioning a JavaScript trick using default parameters to make a parameter mandatory. 


In a language like Apache Groovy, when statically compiled, you'd get a compilation error if you forgot a parameter, because the signature couldn't be found by the compiler. In dynamic mode, you'd get a runtime error though, with a MissingMethodException (and the error message should give you a hint as to which method you should actually call instead). 

But there's a particular case of the Groovy method dispatch that's a bit special (and actually something we might be removing at some point in a breaking version of the language, but it's been there since 1.0). When you have single-argument methods, you're allowed to call those methods without passing a parameter! And the parameter is filled simply with null. So you might have made a mistake in your code, forgetting to pass an actual parameter value, but you'd get neither a compilation error nor a runtime exception, but a null value. 

So I thought about that JavaScript trick, and adapted to this situation, to ensure that you can't call a one-argument method without an argument.

String up(String s) {
    s?.toUpperCase()
}
assert up('groovy') == 'GROOVY'
// a strange aspect of Groovy is that 
// you can call a one-argument method without passing any actual argument
// as if you were passing null, as in up(null)
assert up() == null
// let's use the JavaScript trick with mandatory default params:
// https://twitter.com/djsmith42/status/679018096334675968
String up2(String s = mandatory('s')) {
    s?.toUpperCase()
}
void mandatory(String paramName) {
    throw new Exception("Please provide an actual value for '$paramName'")
}
assert up2('groovy') == 'GROOVY'
try {
    up2()
} catch(emAll) {
    assert emAll.message == "Please provide an actual value for 's'"
}
// another approach with using a closure call as default value
String up3(String s = { -> throw new Exception("Please provide an actual value for 's'") }() ) {
    s?.toUpperCase()
}
assert up3('groovy') == 'GROOVY'
try {
    up3()
} catch(emAll) {
    assert emAll.message == "Please provide an actual value for 's'"
}
I've also pushed that example on the Groovy Web Console, if you wanna play with it.

Groovy Weekly #77

It’s literally the eve of SpringOne2GX, in Washington DC! I’m flying tomorrow, for 9+ hours above the Atlantic, to gather with the cool kids of the Groovy ecosystem. I’m impatient to see some of my fellow readers and Groovy users around there.


And let me wish the Griffon framework and its team a Groovy birthday!

Releases

Articles

Screencasts

Interviews

  • An interview by JaxEnter of Dierk König in German, where he says that Groovy is a pioneer language where innovative changes help shape the future of Java and other languages

Mailing-list posts

Tweets

News

Jobs

Books

Events

Groovy Weekly #76

Is it really already September? Time flies so fast in the Groovy ecosystem, and although the northern hemisphere was taking some Summer vacation breaks (at least that’s what I did!) there was quite a bit of interesting content published!


Regarding events, after successful and busy Greach, GR8Conf US/EU conferences, we’re getting ready for September’s SpringOne2GX in Washington! There’s still time to register and join the Groovy ecosystem affictionados! And I’m really eager to seeing you there.


The Call for Papers for Grails India and Groovy Grails eXchange London are also open, for those who wish to submit presentations.


If you’re using Gradle for build automation, check out all the videos of the presentations of the recent Gradle Summit which should all be available online.


Happy 3rd birthday to GrooScript!

Congrats to the Griffon team for the 2.4 release and Ratpack for its RC before 1.0!


And check out the updated books from MrHaki on Groovy, Grails and Gradle, and Duncan Dickinson’s updated Groovy 2 tutorial on LeanPub.

Releases

Articles

Presentations

Mailing-list posts

Tweets

News

Jobs

Code snippets

Books

Podcasts

Events

Groovy Weekly #75

The not-so-weekly Groovy column is back for a summer edition on the shores of the Atlantic ocean (at least, that’s where I am now!) and there was a lot to catch up with in terms of news!


Ratpack is almost at 1.0, only a couple of weeks to go for the mythical release, whereas Gradle arrived in version 2.6, and some updates to the two main Grails lines were out too.


You’ll notice lots of delightful content from GR8Conf US 2015! So many talks to watch (and slides to look at), if you didn’t get a chance to attend the conference.

Releases

Articles

Interviews

Presentations

Tweets

News

Code snippets

Books

Podcasts

Events

Groovy Weekly #74

Although on a more irregular schedule, Groovy Weekly continues in the summer with its 74th edition, crossing the bar of the 3000 total news items tracked since it all started!


Some interesting releases for this edition, like the Groovy 2.4.4 release under the Apache umbrella! After a failed attempt, our second try was succesful, and it was particularly important to get it right out the door, as an important security issue uncovered was fixed in this release, and all Groovy users (and frameworks and libraries using Groovy) should upgrade as soon as possible — especially as no older version will be released with the patch.


In the release section, last time, I had forgotten Griffon 2.3.0 which was released on stage at GR8Conf Europe by Andrés Almiray. And in other releases, we also have Grails 3.0.3 with important reloading improvements, Geb 0.12.0 which celebrated its 5th anniversary, and also Codenarc 0.24 that I had forgotten the previous edition as well.


It looks like Slack is really becoming very popular as both Grails and Ratpack opened Slack channels!

Releases

Articles

Presentations

Tweets

News

  • Groovy 2.4.3 and below was affected by a security issue that has been fixed in Groovy 2.4.4. Projects should upgrade to 2.4.4 as soon as possible to avoid this security issue to be leveraged by malicious attackers.

  • Jacob Aae Mikkelsen Grails Diary week 25 to 28

  • Jacob Aae Mikkelsen's Grails Diary week 29

  • Mockito is bringing some better support for Groovy classes

Screencasts

Podcasts

Mailing-list posts

 
© 2012 Guillaume Laforge | The views and opinions expressed here are mine and don't reflect the ones from my employer.