Hi sorry I'm new here, but I'm having trouble with...
# compiler
a
Hi sorry I'm new here, but I'm having trouble with the kotlin-assignment-plugin, and it's kinda essential to my DSL. Is this the right channel for compiler plugins? When my dependency includes the assignment plugin, and I add its annotation to my assignment plugin configuration, I still can't use it. It just acts like I'm trying to set the wrong type, even though inside the same project it works fine. Is there any way to let it work between libraries?
a
The kotlin-assigment-plugin must be applied in any project that needs it. It's not enough to only apply it to the project that contains the
fun assign(...)
or annotation.
(Better channels would be #getting-started or #general-advice)
a
The plugin is independently applied in the downstream project, and the marker annotation from the library is added to its configuration as well
a
Ah okay. Hmmm... Is the
fun assign()
an extension function?
a
It's an interface function on a parent interface, and has a context parameter:
Copy code
@UsesAssignmentOperator
interface IItemAttribute<T> {
   context(attrs: IAttributeContainer)
   fun assign(value: T)
}
It doesn't work even without the context parameter, though.
a
yeah, okay. Probably it won't ever work with a context param because the assign plugin requires only one arg. But it should work without it. When you say "it doesn't work" - what does that look like? It's important to check whether it's a problem with the IDE or compiler.
a
When assigning something, it gives the error "Expected ItemAttribute<Number>, but got Number", with no "no assign function found" error like you'd expect
a
it gives the error
does the IDE give the error or does it fail when you run
./gradlew assemble
?
a
The IDE and the compiler both give the error.
Truth be told, I'm starting to wonder if the assignment plugin itself isn't being applied somehow
Not getting the "no assign function found" error makes me wonder
I'm gonna keep trying, thanks for troubleshooting
a
ok cool, please share an update if you manage to fix it
y
FWIW, the assign plugin has some intentional restrictions on
assign
as a deliberate design decisions The extension point in the compiler doesn't have those restrictions, though. I made my own less-restrictive assign plugin way back as a result. It's very out of date now, so I'm not suggesting you use it, but I'm just saying that it's possible to do what you want
a
@Youssef Shoaib [MOD] Cool! What restrictions did you lift? btw do you know if it's possible to prevent the runtime error that happens if you use the overloaded
=
inside an
init {}
block?
y
What I did is practically turn all illegal
foo = bar
assignments into
foo.assign(bar)
, where illegal means that
foo
is not a
var
. It's been a while, but I think a call like
this = bar
never gets transformed because the compiler can't make sense of it Not sure about that runtime error. If I had to guess, it might be because your LHS value wasn't initialized yet? Although I'd expect that the compiler would be able to catch that.
Looks like we've had a similar conversation before lol FWIW, the official assignment plugin deliberately doesn't support the LHS being a local
val
. It also require a special annotation on its type. My plugin lifts both restrictions. Looking into the initialization thing more, it really feels like a bug to me. I have a hypothesis on why it happens (the
val
that the assign plugin returns to the compiler is already resolved, so normal checks like initialization don't happen).