Search code examples
groovycoercion

coercion to interface with methods having multiple parameters


Let's say I've got such interface

interface IFile {
    void writeFile(String name, byte[] bytes)
    byte[] readFile(String name)
}

How to do a coercion to that interface? Because so far that approach does not work and leads to compilation exception

def fileCoeImp = { 
    name, bytes -> new File(nane) << bytes,  
    name -> new File(name).getBytes()
} as IFile 

Solution

  • You need to use a map:

    def fileCoeImp = [
        writeFile : { name, bytes -> new File(name) << bytes },  
        readFile : { name -> new File(name).getBytes() }
    ] as IFile