https://kotlinlang.org logo
l

Lucas

10/13/2022, 1:11 AM
Is there any way to anotate a class in common module with java-only lib anotations? expected/actual didnt work because the anotation has enum inside and i couldnt get it to work
🤔 1
v

Vitor Hugo Schwaab

10/13/2022, 6:26 AM
The main issue are the other platforms, you'd need to implement the actual enum and annotation classes which are only dummys. I wonder if there was a way around it for your case, but interesting problem nonetheless.
b

Big Chungus

10/13/2022, 8:31 AM
Move enum expect out of annotation class and then typealias it on jvm? e.g. typealias MyEnum = Annotation.Enum
v

Vitor Hugo Schwaab

10/13/2022, 8:34 AM
I assumed @Lucas still wants to pass arguments in the Common code and that the source code for the JVM enum is an external dependency. For that the enum must be defined in common code.
Copy code
// ## Common source

@JvmJsonProperty(required = false, access = JsonPropertyAccess.WRITE_ONLY)
b

Big Chungus

10/13/2022, 8:35 AM
You can still do that as long as you typealias it properly on jvm
v

Vitor Hugo Schwaab

10/13/2022, 8:49 AM
Could you please provide examples of what you mean? Cause I think one of us is missing something and there's something to learn here 😅 The JVM implementation is concrete and defined in a JVM library. Let's say it's like:
Copy code
// Some External JVM Library that it's being used in the JVM
annotation class LibAnnotation(val enum: LibEnum)

enum class LibEnum { A, B }
Now, in the common code, we need to call the
LibAnnotation
constructor with the
LibEnum
parameter. Can't do that directly, so expect/actual needed.
Copy code
// Common Code

/**
 * An annotation that will only affect JVM targets
 */
expect annotation class JvmLibAnnotation(val enum: ???)

// This way it can be used like so:
@JvmLibAnnotation(enum = ???)


// JVM Code
actual typealias JvmLibAnnotation = JsonProperty
What to put in the
???
b

Big Chungus

10/13/2022, 9:01 AM
expect enum class LibEnum actual typealias LibEnum = LibAnnotation.LibEnum
v

Vitor Hugo Schwaab

10/13/2022, 9:22 AM
It's not possible to pass the arguments in the common code like that.
Copy code
// Common Code

@JvmLibAnnotation(enum = LibEnum.A) // A is not defined in common, only in JVM
b

Big Chungus

10/13/2022, 9:32 AM
Of course you can. You use the expect declarations instead
That's the whole point of expect/actual and typealiases
v

Vitor Hugo Schwaab

10/13/2022, 9:34 AM
So I really don't get what was the difference between your suggestion and mine
b

Big Chungus

10/13/2022, 9:36 AM
Just re-read it. Missing actual for enum threw me off.
So yes, we were talking about exact same thing, just from different angles
My key point was that you can typealias into nested enums
i.e. actual typealias MyEnum = LibAnnotation.LibEnum
r

Robert Jaros

10/13/2022, 10:41 AM
When talking about expec/actual annotations - is there any way to have default values for annotation parameters when type aliasing to jvm annotations?
l

Lucas

10/13/2022, 12:00 PM
Thanks! I couldnt get it to work exactly the way you did, the line
Copy code
actual typealias JvmJsonProperty = JsonProperty
had errors, but anottating it with @OptionalExpectation and removing the arguments worked. At least it doesnt give me an error, still have to test it. Thanks a lot @Vitor Hugo Schwaab and @Big Chungus
Well actually it has errors on the common source hehe
v

Vitor Hugo Schwaab

10/13/2022, 12:26 PM
Could you share the errors and the code?
l

Lucas

10/13/2022, 1:01 PM
But reading the error i think the expect class just needs more params on the constructor
v

Vitor Hugo Schwaab

10/13/2022, 1:03 PM
Yep, that should be it. I didn't know there were more possible parameters 🙂 I just wrote a dummy annotation on my machine for JVM and worked with that. If the JVM annotation has more parameters, then you need to implement those too, possibly implement multiple constructors too! (If default arguments aren't enough)
r

Robert Jaros

10/13/2022, 1:08 PM
This compiles for me (but I haven't run the app) Common code:
Copy code
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
expect annotation class JsonProperty(
    val value: String,
    val namespace: String,
    val required: Boolean,
    val index: Int,
    val defaultValue: String,
    val access: Access
)

expect enum class Access {
    AUTO, READ_ONLY, WRITE_ONLY, READ_WRITE;
}

// annotation usage (default values not working)
@JsonProperty(value = "", namespace = "", required = true, index = -1, defaultValue = "", access = Access.AUTO)
JVM code:
Copy code
actual typealias Access = JsonProperty.Access
actual typealias JsonProperty = com.fasterxml.jackson.annotation.JsonProperty
l

Lucas

10/13/2022, 1:09 PM
Yeah, i got to the same thing, same problem with default values That is annoying
Might be worth just cloning the library and making the annotations common code 😅 Not really because other libraries will have the same problem so it would get out of hand
Isnt there a way to typealias the constructor with default parameters?
a

Ashutosh Verma

02/18/2023, 2:10 PM
Hey @Lucas, did you figure out a way?
l

Lucas

02/18/2023, 4:00 PM
Not really, only what is on this thread
13 Views