Hi, I'm thinking about writing a compiler plugin t...
# arrow-meta
t
Hi, I'm thinking about writing a compiler plugin to support named parameters in function types. I need it to do the following transformation:
Copy code
@NamedParameters
typealias UpdateUserByToken = (userToken: String, update: (User) -> User) -> Unit
to
Copy code
interface UpdateUserByToken {
    operator fun invoke(userToken: String, update: (User) -> User): Unit

    companion object {
        operator fun invoke(impl: (userToken: String, update: (User) -> User) -> Unit) = object: UpdateUserByToken {
            override fun invoke(userToken: String, update: (User) -> User) = impl(userToken, update)
        }
    }
}
So you can stuff like this:
Copy code
val updateUserByToken = UpdateUserByToken { userToken, update ->
    // implement
}

[...]

updateUserByToken(userToken = "1234") {
    user.copy(email = "<mailto:foo@bar.com|foo@bar.com>")
}
Is this possibly with arrow-meta? How could I distribute this plugin to my colleagues? Thanks in advance!
s
I am probably missing something, but named parameters are already supported in lambda types?
t
Not on the calling site:
Copy code
updateUserByToken(userToken = "1234") {
    user.copy(email = "<mailto:foo@bar.com|foo@bar.com>")
}
does not work currently
message has been deleted
Another benefit/drawback would be that these interface based functional types are typed by name instead of signature
s
Ah, got it. You certainly can transform the code either by using quote transforms or generating the bytecode / descriptors yourself. See hello world example with transform here: https://github.com/arrow-kt/arrow-meta/blob/master/compiler-plugin/src/main/kotlin/arrow/meta/plugins/helloWorld/DummyPlugin.kt