Issue Details (XML | Word | Printable)

Key: GROOVY-1687
Type: Bug Bug
Status: Closed Closed
Resolution: Won't Fix
Priority: Minor Minor
Assignee: Jochen Theodorou
Reporter: Gavin Grover
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

One-arg methods have implicit default parameter of null

Created: 31/Jan/07 10:30 AM   Updated: 14/Apr/08 06:33 AM   Resolved: 14/Apr/08 06:33 AM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.5.5

Time Tracking:
Not Specified

Environment: v1.0 on Java 1.6.0 on WinXP prof edn


 Description  « Hide

When a method, whether static or instance, has one and only one parameter of non-primitive type, it has an implicit default value of null. Is this intended?

class A{
  static g2( Integer i ){ i }
  def g3( Integer i ){ i }
  static g4( int i ){ i }
  static g5( Integer i, Integer j ){ [ i, j ] }
}

//When no args supplied for one arg static method, default value of null assumed...
assert A.g2( new Integer(7) ) == 7
assert A.g2() == null

//Ditto for instance method...
assert new A().g3( new Integer(8) ) == 8
assert new A().g3() == null

//If parameter is primitive type, argument is mandatory as in Java...
assert A.g4( 7 ) == 7
try{ A.g4(); assert false }catch(Throwable e){ assert e instanceof IllegalArgumentException }

//When there's more than one parameter, both arguments also mandatory...
assert A.g5( new Integer(7), new Integer(8) ) == [ 7, 8 ]
try{ A.g5( new Integer(7) ); assert false }catch(Throwable e){ assert e instanceof MissingMethodException }
try{ A.g5(); assert false }catch(Throwable e){ assert e instanceof MissingMethodException }


Colin Howe added a comment - 22/Feb/08 11:19 AM

Can anyone let us know if this is a design decision or not?

We're encountering problems when our developers incorrectly call a function without parameters. They get unexpected behaviour due to forgetting to pass in a parameter and this can take some time to track down when the function allows null as a parameter.


Jochen Theodorou added a comment - 14/Apr/08 06:33 AM

it was a design decision.. before Groovy 2.0 there is no way to change this