简体   繁体   English

Grails / Groovy:了解闭包代码的参数

[英]Grails/Groovy: Understanding the parameters of the closure code

I'm new to Grails/Groovy, and so please bear with me as I try to understand a piece of code that I had come across in a book. 我是Grails / Groovy的新手,所以在尝试理解我在书中遇到的一段代码时,请多多包涵。

It is a simple Album domain class: 这是一个简单的Album域类:

class Album {
    String artist
    String title
    List songs = []
    List durations = []


    static constraints = {
        artist blank: false
        title blank: false
        songs minSize:1, validator:{ val, obj ->
            if(val.size() != obj.durations.size())
                return "songs.durations.not.equal.size"
    }
}  

My question comes from the constraints property block of code. 我的问题来自代码的constraints属性块。
In the validator constraint, the author uses a closure. 在验证者约束中,作者使用闭包。
But what exactly are "val" and "obj"? 但是“ val”和“ obj”到底是什么? What values will they be given? 他们会得到什么价值?

Also, a bonus question, what type is "constraints"? 另外,还有一个奖励问题,“约束”是什么类型? I don't think it is a map as they are defined as [ ] in Groovy. 我不认为这是一张地图,因为它们在Groovy中被定义为[]。 Coming from a Java perspective, x = { .. } is an array, but I'm not sure it is the same in Groovy. 从Java的角度来看, x = { .. }是一个数组,但是我不确定Groovy中的数组是否相同。

Thanks for the help! 谢谢您的帮助!

Your first question, val and obj parameters refer to the value of the property and a pointer to the instance, respectively. 第一个问题, valobj参数分别引用属性的值和实例的指针。 The documentation for custom validation routines explains this in further detail. 自定义验证例程的文档对此进行了更详细的说明。

As for the bonus question constraints is a Groovy closure . 至于奖金问题的constraints是Groovy 闭包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM