2

How come I cannot declare an array of People in Groovy as shown. Maybe I'm lacking the deeper understanding of classes

class People {
    Integer id
}

class Job {
    def func() {
        People[] p = new People[10]
    }
}

I get an error of People[] cannot be applied to app.People[]

1
  • Unfortunately that is just the nature of the language it looks like... in order to allocate an object that is not on the stack, (Or it's size is predeterministic) -- you must use the def keyword Commented Jun 16, 2018 at 4:38

3 Answers 3

4

The code sample you have shown does not reproduce the error you mentioned in the question above. It's broken actually and does not compile - method func() is missing its body. If you correct the code to e.g.

class People {
    Integer id
}

class Job {
    def func() {
        People[] p = new People[10]
        assert p.size() == 10
        println p
    }
}

new Job().func()​

you will see it produces the expected result - check it out in the Groovy web console here. When you run it you will see following output to the console:

[null, null, null, null, null, null, null, null, null, null]

The difference between Groovy and Java

When it comes to array initialization there is one significant difference between Groovy and Java. In Java you can initialize an array of People[] like this:

People[] p = new People[] { new People(), new People(), /* ... */ new People() };

It wont work in Groovy, because Groovy reserves {} for closures. In Groovy you can initialize such array as:

People[] p = [new People(), new People(), new People()] as People[]
Sign up to request clarification or add additional context in comments.

1 Comment

Hmm I did have brackets in my code, just didn't include in the question. Thanks for the clarification though!
2

While Szymon Stepniak's answer is correct for Groovy 2.5 and below, Java-style array initialization are part of the enhancements of Groovy 3.0 and 2.6 made possible by the new parrot parser.

Example from the release notes:

def primes = new int[] {2, 3, 5, 7, 11}
assert primes.size() == 5 && primes.sum() == 28
assert primes.class.name == '[I'

def pets = new String[] {'cat', 'dog'}
assert pets.size() == 2 && pets.sum() == 'catdog'
assert pets.class.name == '[Ljava.lang.String;'

// traditional Groovy alternative still supported
String[] groovyBooks = [ 'Groovy in Action', 'Making Java Groovy' ]
assert groovyBooks.every{ it.contains('Groovy') }

Comments

0

Szymon Stepniak's answer is correct. I'll point another example of a real case in some unit test that I've worked (general Object type):

Object[] o = [YourModel] as Object[]

This is sufficient to mock a general Object with your model properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.