Created
July 11, 2012 08:47
-
-
Save mike-neck/3089091 to your computer and use it in GitHub Desktop.
Closureを受けた変数名を取ってくる / retrieve a variable name for Closure object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def closure = { | |
| println 'Hoge' | |
| } | |
| class BokuUsamimi { | |
| def kaworichan = { | |
| println "I love you!" | |
| } | |
| } | |
| void hoge (Closure closure) { | |
| def map = closure.delegate.properties.collectEntries { | |
| [it.value, it.key] | |
| } | |
| def name = map[closure] | |
| print "${name}, " | |
| closure.call() | |
| } | |
| hoge new BokuUsamimi().kaworichan |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UsaMimi { | |
| def kaworiChan = {println 'I love you!'} | |
| def kyon = {Closure closure -> | |
| def map = closure.delegate.properties.collectEntries { | |
| [it.value, it.key] | |
| } | |
| def name = map[closure] | |
| print "${name} " | |
| closure () | |
| } | |
| void api () { | |
| kyon (kaworiChan) | |
| } | |
| } | |
| def usa = new UsaMimi () | |
| usa.api () |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| groovy> class UsaMimi { | |
| groovy> def kaworiChan = {println 'I love you!'} | |
| groovy> def kyon = {Closure closure -> | |
| groovy> def map = closure.delegate.properties.collectEntries { | |
| groovy> [it.value, it.key] | |
| groovy> } | |
| groovy> def name = map[closure] | |
| groovy> print "${name} " | |
| groovy> closure () | |
| groovy> } | |
| groovy> void api () { | |
| groovy> kyon (kaworiChan) | |
| groovy> } | |
| groovy> } | |
| groovy> def usa = new UsaMimi () | |
| groovy> usa.api () | |
| kaworiChan I love you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BokuUsamimi { | |
| def kaworiChan = { | |
| println "I love you!" | |
| } | |
| void mimi (Closure closure) { | |
| def map = closure.delegate.properties.collectEntries { | |
| [it.value, it.key] | |
| } | |
| def name = map[closure] | |
| print "${name} " | |
| closure () | |
| } | |
| } | |
| def usaNo = new BokuUsamimi () | |
| usaNo.mimi usaNo.kaworiChan |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment