title: Is there a possibility to serialize Kotlin Data class with function literal?
body: <p>I have following setup:</p>
  
  <p>Kotlin data class:</p>
  
  <pre><code>data class Error(val throwable: Throwable, val reloadAction: (() -&gt; Unit)? = null) : Serializable
  </code></pre>
  
  <p>Initialization of class:</p>
  
  <pre><code>val instance = Error(throwable,this::loadData)
  </code></pre>
  
  <p>Where <code>this</code> is <code>ViewModel</code> from Android Architecture components</p>
  
  <p>When I'm passing this data class to parcelable:</p>
  
  <pre><code>parcel.writeSerializable(instance as Serializable)
  </code></pre>
  
  <p>I get following exception:</p>
  
  <pre><code>java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.mypackagename.Error)
          at android.os.Parcel.writeSerializable(Parcel.java:1468)
          at com.mypackagename.CustomView$Companion$SavedState.writeToParcel(CustomView.kt:177)
  ...
  Caused by: java.io.NotSerializableException: com.mypackagename.ExampleViewModel
  </code></pre>
  
  <p>In this question: <a href="https://stackoverflow.com/questions/45617564/is-there-a-way-to-pass-a-function-reference-between-activities">Is there a way to pass a function reference between activities?</a> is following answer:</p>
  
  <blockquote>
    <p>It will actually be Serializable if it is a function reference (SomeClass::someFunction) or a lambda. But it might not be, if it is some custom implementation of the functional interface () -> Unit, so you should check that and handle the cases when it's not.</p>
  </blockquote>
  
  <p>Is this my case? Or data class cannot be serialized when function is function of non serializable object (ViewModel)? Are there any other possibilities how to pass function literal as Serializable inside data class?</p>