thanh
06/02/2021, 10:07 AMrefined-type
plugin.
here is an example:
@JvmInline
@Serializable
value class GameId private constructor(val value: String) {
companion object : Refined<String, GameId>(::GameId, {
ensure((it.length == 8) to "Expected $it has 8 characters")
})
}
@Serializable
data class Game(
val gameId: GameId,
val color: String,
)
fun main() {
val json = """{"gameId":"abcd1234","color":"white"}"""
println(Json.decodeFromString<Game>(json))
val json2 = """{"gameId":"","color":"white"}"""
println(Json.decodeFromString<Game>(json2))
}
Output:
Game(gameId=GameId(value=abcd1234), color=white)
Game(gameId=GameId(value=), color=white)
Is this something we have to accept or should we put some require in init
block?raulraja
06/02/2021, 10:26 AMvalue class GameId private constructor(val value: String) {
init {
GameId.require(value)
}
companion object : Refined<String, GameId>(::GameId, {
ensure((it.length == 8) to "Expected $it has 8 characters")
})
}
thanh
06/02/2021, 12:27 PMGameId.require(value)
inside init
like that it would cause StackOverFlow like this:
xception in thread "main" java.lang.StackOverflowError
at se.thanh.refined.GameId$Companion$2.invoke(Main.kt)
at se.thanh.refined.GameId$Companion$2.invoke(Main.kt:27)
at se.thanh.refined.GameId.constructor-impl(Main.kt:65)
at se.thanh.refined.GameId.access$constructor-impl(Main.kt:17)
at se.thanh.refined.GameId$Companion$1.invoke-4eQqh7M-4eQqh7M(Main.kt:27)
at se.thanh.refined.GameId$Companion$1.invoke(Main.kt:27)
at se.thanh.refined.GameId.constructor-impl(Main.kt:66)
at se.thanh.refined.GameId.access$constructor-impl(Main.kt:17)
at se.thanh.refined.GameId$Companion$1.invoke-4eQqh7M-4eQqh7M(Main.kt:27)
thanh
06/02/2021, 12:28 PMinit {
val results = constraints(value)
if (!results.allValid())
throw IllegalArgumentException(renderMessages(results))
}
thanh
06/02/2021, 12:29 PMthanh
06/02/2021, 12:35 PMrefined-type
plugin.raulraja
06/02/2021, 1:48 PMthanh
06/02/2021, 2:43 PMthanh
06/02/2021, 2:49 PMlistOf("").map { GameId(it) }.forEach(::println)
The code above is compiled successfully and an IllegalStateException
is thrown. I thought refined-type
should not allow this kind of behavior?raulraja
06/02/2021, 3:22 PMit
is not a constant value. https://github.com/arrow-kt/arrow-meta/blob/main/refined-types-plugin/src/test/kotlin/arrow/meta/plugins/liquid/LiquidTests.kt#L108-L118raulraja
06/02/2021, 3:23 PMthanh
06/02/2021, 3:41 PMraulraja
06/03/2021, 7:41 AMthanh
06/03/2021, 9:49 AMfun f(n: Int) =
PositiveInt(n)
// this should not be compiled
fun useDynamicValue() {
val z = f(-1)
println(z)
// Output
// Exception in thread "main" java.lang.IllegalArgumentException: -1 should be > 0
// at se.thanh.refined.MainKt.f(Main.kt:52)
// at se.thanh.refined.MainKt.useDynamicValue(Main.kt:28)
// at se.thanh.refined.MainKt.main(Main.kt:14)
// at se.thanh.refined.MainKt.main(Main.kt)
}
I pushed a sample code in github, can you please take a look: https://github.com/lenguyenthanh/arrrow-refined-types-example.raulraja
06/03/2021, 12:07 PMraulraja
06/03/2021, 12:07 PMthanh
06/03/2021, 12:10 PMRachel
06/03/2021, 4:53 PMkapt
plugin interferes with the proper behavior of the Refined Types Plugin.
If I just add kapt
plugin into demos , Refined Types Plugin doesn't work as expected in refined-types-compiler-plugin-demo
.
We'll review the interaction with other plugins, thank you so much for letting us know!!raulraja
06/03/2021, 5:02 PMthanh
06/03/2021, 5:21 PMdata class PositiveIntEven private constructor(val value: Int) {
companion object : Refined<Int, PositiveIntEven>(::PositiveIntEven, PositiveInt, Even)
}
fun main() {
val n = -1
val result = PositiveInt(n).value
// val result = PositiveInt(-1).value
}
It is still compiled and throw an exception in runtime.Rachel
06/10/2021, 9:06 AMarrow-meta/refined-types-plugin/src/test/kotlin/arrow/meta/plugins/liquid/LiquidTests.kt
An issue about it would be very welcome 🙏
Thank you so much!! 🙌thanh
06/10/2021, 10:43 AMRachel
06/10/2021, 11:15 AM