My `data class`es don’t seem to have a `copy` func...
# javascript
r
My `data class`es don’t seem to have a
copy
function when I run my app, any idea why? Would DCE remove it or something? What can I do?
Is nobody using data classes in Kotlin/JS?
g
Hum, what I know about Kotlin JS is that we don’t have everything there =/ For example, I was trying to use some reflection methods but we don’t have the entire java language there, afaik
r
Not having reflection is expected, it’s a JVM thing, but data classes’ copy method is a Kotlin thing and also their main interest so I expect them to work
r
@ribesg what do you mean? You can not call
.copy()
? Or your compiled code fails at runtime?
Or perhaps you try to call .copy() directly on JavaScript object?
r
The IDE has no problem with me using them, but at runtime it doesn’t find the copy function (with or without DCE)
t
i agree in parts...
r
I’ll link my project, it’s open source
t
my data classes DO decalre a copy function but its name mangeled
in the end it looks like
IStrukturstellenAufbauChange.prototype.copy_qa0fef$
while e.g. the generated equals methods are not mangeled
Yes the methods are mangled
Copy code
Uncaught TypeError: this.state.copy_qz9155$ is not a function
r
you are calling copy on a
state
variable, but imho this variable is not a kotlin data class
it just pretends a data class 😉
That doesn’t count?
Looks like you’re right but I wonder why. I tried this
Copy code
setState((state as State).copy(loginInput = text))
IntelliJ says the cast is useless but it throw a cast exception
r
I don't know. Perhaps the state is wrapped somehow by React.
I don't use react so I'm not sure how it works.
t
the type ssytem of kotlin is really missleading here. you can any object to be of any type in javascript. i once ran into a
NoWhenBranchMatchedException
because of that...
in that case i got an argument with type of a sealed class and used the typical
when
expression to find out it's type. well - nothing matches because it was basically
{}
r
There are 3 different
setState
functions, I looked at example and they’re using States with
var
properties... I don’t really like it but I guess it’s how it’s supposed to be done?
z
It might be because you are initializing the state incorrectly. I would try
Copy code
fun State.init() {
   loginInput = ""
  passwordInput = "",
  isSignUpMode = false
}
I'm not sure if the constructor gets called the way you are expecting it
You could also make this a functional component very easily and the initialization of the state would make way more sense. If you'd like I can create a gist to show you how I would do it.
I don’t like the fact that my state is no longer immutable, the code block you wrote also does not seem to define properties with
val
. I’m a React newbie so I don’t know how you’re supposed to initialize the state in a class component. Is a functional component when you use
useState
?
z
Correct. I just submitted a pull request as an example of how I would do it.
The pull request probably isn't suitable for merge though.
A functional component is a component that has no state and doesn't extend from
RComponent
in this case. Hooks give us a way around that, and
useState
is one of those hooks. I've found that
useReducer
can be extremely useful as well, if you are interested.
In my projects, I try to avoid the traditional way of creating components by extending from RComponent as that seems to lead to mutable state. I prefer the more functional approach with functional components and hooks.
👍 2
r
That’s very interesting, it looks much better! Thanks a lot
g
@Zachary Grafton have you had a situation where you needed a traditional component instead of a functional one?
z
No. I know there are a few cases where you might need one, but I have never ran into them myself.