You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by smallufo <sm...@gmail.com> on 2025/03/17 10:46:48 UTC

Kotlin 2 unable to produce Serializable Lambda

private val nameField: TextField<String> = TextField("name",
model.flatMap { m -> LambdaModel.of( { m.name }, { m.name = it } ) })

(with wicket 10)
The old code works well with Kotlin 1.9
But after upgrading kotlin to 2.x

It shows exception :
private java.lang.Object
org.apache.wicket.MarkupContainer.children[write:1]
[class=org.apache.wicket.markup.html.form.TextField, path=3:form:name]
java.lang.Object org.apache.wicket.Component.data
[class=java.lang.Object] final
org.danekja.java.util.function.serializable.SerializableFunction
org.apache.wicket.model.IModel$4.val$mapper [class=java.lang.Object]

It seems Kotlin 2 no longer produce serializable lambda.
I tried these ways :

class SerializableLambdaModel<T>(
    private val getter: () -> T,
    private val setter: (T) -> Unit
) : IModel<T>, java.io.Serializable {

    override fun getObject(): T = getter()

    override fun setObject(value: T) {
        setter(value)
    }

    override fun detach() {
    }
}
not working

and this

class SerializableLambdaModel<T>(
  getter: () -> T,
  setter: (T) -> Unit
) : IModel<T>, java.io.Serializable {

  private val getterFunc: () -> T = object : java.io.Serializable {
    override fun toString(): String = getter().toString()
    fun get(): T = getter()
  }

  private val setterFunc: (T) -> Unit = object : java.io.Serializable {
    fun set(value: T) = setter(value)
  }

  override fun getObject(): T = getterFunc.get()

  override fun setObject(value: T) {
    setterFunc.set(value)
  }

  override fun detach() {}
}

and this

import org.apache.wicket.model.IModel
import java.io.Serializable

class SerializableLambdaModel<T : Any?>(
    private val getter: () -> T,
    private val setter: (T) -> Unit
) : IModel<T>, Serializable {

    private val getterFunc: SerializableFunction0<T> =
SerializableFunction0(getter)
    private val setterFunc: SerializableFunction1<T> =
SerializableFunction1(setter)

    override fun getObject(): T {
        return getterFunc.invoke()
    }

    override fun setObject(value: T) {
        setterFunc.invoke(value)
    }

    override fun detach() {
    }

    private class SerializableFunction0<T>(private val function: () ->
T) : () -> T, Serializable {
        override fun invoke(): T = function()
    }

    private class SerializableFunction1<T>(private val function: (T)
-> Unit) : (T) -> Unit, Serializable {
        override fun invoke(value: T) = function(value)
    }
}

ALL not working , throws the same error

Is there any way to solve this ?

Thanks.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Kotlin 2 unable to produce Serializable Lambda

Posted by smallufo <sm...@gmail.com>.
Thank you.
It works now!

Johan Stuyts <jo...@squins.com> 於 2025年3月18日 週二 上午12:13寫道:
>
> The way code for lambdas is generated was changed in 2.0. You have to switch back to the legacy implementation : https://kotlinlang.org/docs/whatsnew20.html#generation-of-lambda-functions-using-invokedynamic
>
> In your build.gradle.kts:
>
> tasks.withType<KotlinCompile> {
>      compilerOptions {
>          freeCompilerArgs = listOf(<any other options that you need>, "-Xlambdas=class")
>      }
> }
>
>
> Op Mon, 17 Mar 2025 11:46:48 +0100 schreef smallufo <sm...@gmail.com>:
>
> > private val nameField: TextField<String> = TextField("name",
> > model.flatMap { m -> LambdaModel.of( { m.name }, { m.name = it } ) })
> >
> > (with wicket 10)
> > The old code works well with Kotlin 1.9
> > But after upgrading kotlin to 2.x
> >
> > It shows exception :
> > private java.lang.Object
> > org.apache.wicket.MarkupContainer.children[write:1]
> > [class=org.apache.wicket.markup.html.form.TextField, path=3:form:name]
> > java.lang.Object org.apache.wicket.Component.data
> > [class=java.lang.Object] final
> > org.danekja.java.util.function.serializable.SerializableFunction
> > org.apache.wicket.model.IModel$4.val$mapper [class=java.lang.Object]
> >
> > It seems Kotlin 2 no longer produce serializable lambda.
> > I tried these ways :
> >
> > class SerializableLambdaModel<T>(
> >     private val getter: () -> T,
> >     private val setter: (T) -> Unit
> > ) : IModel<T>, java.io.Serializable {
> >
> >     override fun getObject(): T = getter()
> >
> >     override fun setObject(value: T) {
> >         setter(value)
> >     }
> >
> >     override fun detach() {
> >     }
> > }
> > not working
> >
> > and this
> >
> > class SerializableLambdaModel<T>(
> >   getter: () -> T,
> >   setter: (T) -> Unit
> > ) : IModel<T>, java.io.Serializable {
> >
> >   private val getterFunc: () -> T = object : java.io.Serializable {
> >     override fun toString(): String = getter().toString()
> >     fun get(): T = getter()
> >   }
> >
> >   private val setterFunc: (T) -> Unit = object : java.io.Serializable {
> >     fun set(value: T) = setter(value)
> >   }
> >
> >   override fun getObject(): T = getterFunc.get()
> >
> >   override fun setObject(value: T) {
> >     setterFunc.set(value)
> >   }
> >
> >   override fun detach() {}
> > }
> >
> > and this
> >
> > import org.apache.wicket.model.IModel
> > import java.io.Serializable
> >
> > class SerializableLambdaModel<T : Any?>(
> >     private val getter: () -> T,
> >     private val setter: (T) -> Unit
> > ) : IModel<T>, Serializable {
> >
> >     private val getterFunc: SerializableFunction0<T> =
> > SerializableFunction0(getter)
> >     private val setterFunc: SerializableFunction1<T> =
> > SerializableFunction1(setter)
> >
> >     override fun getObject(): T {
> >         return getterFunc.invoke()
> >     }
> >
> >     override fun setObject(value: T) {
> >         setterFunc.invoke(value)
> >     }
> >
> >     override fun detach() {
> >     }
> >
> >     private class SerializableFunction0<T>(private val function: () ->
> > T) : () -> T, Serializable {
> >         override fun invoke(): T = function()
> >     }
> >
> >     private class SerializableFunction1<T>(private val function: (T)
> > -> Unit) : (T) -> Unit, Serializable {
> >         override fun invoke(value: T) = function(value)
> >     }
> > }
> >
> > ALL not working , throws the same error
> >
> > Is there any way to solve this ?
> >
> > Thanks.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> > For additional commands, e-mail: users-help@wicket.apache.org
> >
> >
>
>
> --
> Best regards / Met vriendelijke groet,
>
> Johan Stuyts
> Squins IT Solutions BV
> Oranjestraat 30
> 2983 HS  Ridderkerk
> The Netherlands
> www.squins.com
> Chamber of commerce Rotterdam: 24435103
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Kotlin 2 unable to produce Serializable Lambda

Posted by Johan Stuyts <jo...@squins.com>.
The way code for lambdas is generated was changed in 2.0. You have to switch back to the legacy implementation : https://kotlinlang.org/docs/whatsnew20.html#generation-of-lambda-functions-using-invokedynamic

In your build.gradle.kts:

tasks.withType<KotlinCompile> {
     compilerOptions {
         freeCompilerArgs = listOf(<any other options that you need>, "-Xlambdas=class")
     }
}


Op Mon, 17 Mar 2025 11:46:48 +0100 schreef smallufo <sm...@gmail.com>:

> private val nameField: TextField<String> = TextField("name",
> model.flatMap { m -> LambdaModel.of( { m.name }, { m.name = it } ) })
>
> (with wicket 10)
> The old code works well with Kotlin 1.9
> But after upgrading kotlin to 2.x
>
> It shows exception :
> private java.lang.Object
> org.apache.wicket.MarkupContainer.children[write:1]
> [class=org.apache.wicket.markup.html.form.TextField, path=3:form:name]
> java.lang.Object org.apache.wicket.Component.data
> [class=java.lang.Object] final
> org.danekja.java.util.function.serializable.SerializableFunction
> org.apache.wicket.model.IModel$4.val$mapper [class=java.lang.Object]
>
> It seems Kotlin 2 no longer produce serializable lambda.
> I tried these ways :
>
> class SerializableLambdaModel<T>(
>     private val getter: () -> T,
>     private val setter: (T) -> Unit
> ) : IModel<T>, java.io.Serializable {
>
>     override fun getObject(): T = getter()
>
>     override fun setObject(value: T) {
>         setter(value)
>     }
>
>     override fun detach() {
>     }
> }
> not working
>
> and this
>
> class SerializableLambdaModel<T>(
>   getter: () -> T,
>   setter: (T) -> Unit
> ) : IModel<T>, java.io.Serializable {
>
>   private val getterFunc: () -> T = object : java.io.Serializable {
>     override fun toString(): String = getter().toString()
>     fun get(): T = getter()
>   }
>
>   private val setterFunc: (T) -> Unit = object : java.io.Serializable {
>     fun set(value: T) = setter(value)
>   }
>
>   override fun getObject(): T = getterFunc.get()
>
>   override fun setObject(value: T) {
>     setterFunc.set(value)
>   }
>
>   override fun detach() {}
> }
>
> and this
>
> import org.apache.wicket.model.IModel
> import java.io.Serializable
>
> class SerializableLambdaModel<T : Any?>(
>     private val getter: () -> T,
>     private val setter: (T) -> Unit
> ) : IModel<T>, Serializable {
>
>     private val getterFunc: SerializableFunction0<T> =
> SerializableFunction0(getter)
>     private val setterFunc: SerializableFunction1<T> =
> SerializableFunction1(setter)
>
>     override fun getObject(): T {
>         return getterFunc.invoke()
>     }
>
>     override fun setObject(value: T) {
>         setterFunc.invoke(value)
>     }
>
>     override fun detach() {
>     }
>
>     private class SerializableFunction0<T>(private val function: () ->
> T) : () -> T, Serializable {
>         override fun invoke(): T = function()
>     }
>
>     private class SerializableFunction1<T>(private val function: (T)
> -> Unit) : (T) -> Unit, Serializable {
>         override fun invoke(value: T) = function(value)
>     }
> }
>
> ALL not working , throws the same error
>
> Is there any way to solve this ?
>
> Thanks.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Best regards / Met vriendelijke groet,

Johan Stuyts
Squins IT Solutions BV
Oranjestraat 30
2983 HS  Ridderkerk
The Netherlands
www.squins.com
Chamber of commerce Rotterdam: 24435103

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Kotlin 2 unable to produce Serializable Lambda

Posted by Jonathan Locke <jo...@thanlocke.com>.
If you extend IModel<T>, you’re already extending Serializable, so there’s no need to do any of the things you tried.

The stack trace doesn’t look complete to me. Can you post the full stack trace including line numbers?

Also, a reproduce case could be helpful.

Thanks,

Jon

> On Mar 17, 2025, at 4:46 AM, smallufo <sm...@gmail.com> wrote:
> 
> private val nameField: TextField<String> = TextField("name",
> model.flatMap { m -> LambdaModel.of( { m.name }, { m.name = it } ) })
> 
> (with wicket 10)
> The old code works well with Kotlin 1.9
> But after upgrading kotlin to 2.x
> 
> It shows exception :
> private java.lang.Object
> org.apache.wicket.MarkupContainer.children[write:1]
> [class=org.apache.wicket.markup.html.form.TextField, path=3:form:name]
> java.lang.Object org.apache.wicket.Component.data
> [class=java.lang.Object] final
> org.danekja.java.util.function.serializable.SerializableFunction
> org.apache.wicket.model.IModel$4.val$mapper [class=java.lang.Object]
> 
> It seems Kotlin 2 no longer produce serializable lambda.
> I tried these ways :
> 
> class SerializableLambdaModel<T>(
>    private val getter: () -> T,
>    private val setter: (T) -> Unit
> ) : IModel<T>, java.io.Serializable {
> 
>    override fun getObject(): T = getter()
> 
>    override fun setObject(value: T) {
>        setter(value)
>    }
> 
>    override fun detach() {
>    }
> }
> not working
> 
> and this
> 
> class SerializableLambdaModel<T>(
>  getter: () -> T,
>  setter: (T) -> Unit
> ) : IModel<T>, java.io.Serializable {
> 
>  private val getterFunc: () -> T = object : java.io.Serializable {
>    override fun toString(): String = getter().toString()
>    fun get(): T = getter()
>  }
> 
>  private val setterFunc: (T) -> Unit = object : java.io.Serializable {
>    fun set(value: T) = setter(value)
>  }
> 
>  override fun getObject(): T = getterFunc.get()
> 
>  override fun setObject(value: T) {
>    setterFunc.set(value)
>  }
> 
>  override fun detach() {}
> }
> 
> and this
> 
> import org.apache.wicket.model.IModel
> import java.io.Serializable
> 
> class SerializableLambdaModel<T : Any?>(
>    private val getter: () -> T,
>    private val setter: (T) -> Unit
> ) : IModel<T>, Serializable {
> 
>    private val getterFunc: SerializableFunction0<T> =
> SerializableFunction0(getter)
>    private val setterFunc: SerializableFunction1<T> =
> SerializableFunction1(setter)
> 
>    override fun getObject(): T {
>        return getterFunc.invoke()
>    }
> 
>    override fun setObject(value: T) {
>        setterFunc.invoke(value)
>    }
> 
>    override fun detach() {
>    }
> 
>    private class SerializableFunction0<T>(private val function: () ->
> T) : () -> T, Serializable {
>        override fun invoke(): T = function()
>    }
> 
>    private class SerializableFunction1<T>(private val function: (T)
> -> Unit) : (T) -> Unit, Serializable {
>        override fun invoke(value: T) = function(value)
>    }
> }
> 
> ALL not working , throws the same error
> 
> Is there any way to solve this ?
> 
> Thanks.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org