Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curly brace vs Square bracket

Tags:

grails

groovy

I can't figure out what's the difference between Curly brace and Square bracket in Groovy/Grails Example :

[bookInstanceList:Book.list()]

and :

{
    subject blank: false
    content blank: false, maxSize: 2000
}  

can any one help me please?
Thank you

like image 424
hereForLearing Avatar asked Oct 18 '25 01:10

hereForLearing


2 Answers

Groovy List and Map :

First one is Map. [] (Square bracket) in groovy is used for making list or map.

Example of List:

  • [] - An empty list
  • [1,2,3,4] – A list of integer values
  • [‘Angular’, ‘Groovy’, ‘Java’] – A list of Strings
  • [1, 2, [3, 4], 5] – A nested list

Example of Map:

  • [ : ] – An Empty map.
  • [key: "value"] - Map with key and values

Groovy Closure :

The second one is groovy DSL. We can use multiple strategies to create DSL but in Grails domain constraint blocks used groovy closures for this. You can find more details about DSL here and closures here.

like image 124
Jeevesh pandey Avatar answered Oct 19 '25 16:10

Jeevesh pandey


As pointed out in the comments the first is a Map and the second is a closure.

They aren't the same or similar in any way. You seem to be confused because you assume the closure is some type of name value pair. Which, in this case, it would appear to be because of the constraints DSL.

To further understand how this closure is processed you would need to dig deeper into the constraints DSL and see how it uses such things as missing methods and missing properties. It's not a simple subject to explain briefly.

like image 24
Joshua Moore Avatar answered Oct 19 '25 17:10

Joshua Moore