1

I am trying to use unsigned types in Kotlin. I am using kotlin verison 1.4.32

I have added the compiler option -Xuse-experimental=kotlin.ExperimentalUnsignedTypes as specified in the documentation.

but I am getting this error: Type of the constant expression cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath

tried the answers here with no luck.

here is my code

@ExperimentalUnsignedTypes
enum class Status(val value: UByte) {
    None(0x00u)
}

I have now inside build.gradle file

allprojects {

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
        kotlinOptions {
            freeCompilerArgs += [
                    "-Xuse-experimental=kotlin.ExperimentalUnsignedTypes",
                    "-XXLanguage:+InlineClasses"
            ]
        }
    }
}

Am I missing something?

Remon Shehatta
  • 1,324
  • 1
  • 10
  • 21
  • 3
    Can you show your code? – Sweeper Sep 16 '21 at 13:30
  • @Sweeper I added the code that I have in Gradle. do you need source code as well? – Remon Shehatta Sep 16 '21 at 14:12
  • 1
    I think the question was mostly about the code that utilizes unsigned types ;-) What version of Kotlin do you use? Unsigned types are stable since Kotlin 1.5 and they don't require any compiler flags, etc. anymore - they should *just work*. – broot Sep 16 '21 at 14:18
  • unfortunately, I have to use 1.4. I updated the question. please check. – Remon Shehatta Sep 16 '21 at 14:35
  • Do you get this error in IDE only or also when running gradle from the command line? – Joffrey Sep 16 '21 at 14:54
  • from the ide only. I just ran `./gradlew assembleDebug -p app` and it said build successful. weird. – Remon Shehatta Sep 16 '21 at 15:01
  • hold on..`./gradlew assembleDebug ` is failing as well :( – Remon Shehatta Sep 16 '21 at 15:06
  • @RemonShehatta please, provide the complete compiler output and the code mentioned in it. The snippet in your current question seems to be correct. – Commander Tvis Sep 17 '21 at 04:37
  • I mean, each compiler error in Kotlin relates to a certain location in your code; e.g. trying to compile `fun main() = println(nothing)` leads to an error in the compiler output `e: something.kt: (1, 22): Unresolved reference: nothing` which means that wrong code starts on line 1, row 22 in the file `something.kt`. – Commander Tvis Sep 17 '21 at 04:43
  • I have just created a new project in Android Studio with the configurations I mentioned above (Kotlin version and adding compiler arguments) and it gave me the exact error. posting the full error here: `e: /Users/remon.shehata/AndroidStudioProjects/UnsignedTypes/app/src/main/java/com/example/unsignedtypes/MainActivity.kt: (15, 10): Type of the constant expression cannot be resolved. Please make sure you have the required dependencies for unsigned types in the classpath` – Remon Shehatta Sep 20 '21 at 11:53

0 Answers0