poohbar
08/15/2022, 7:36 PMif (obj instanceOf String myString) {
// myString
}
I know I can do as and get a smart cast but I can’t give it a new name in Kotlin, right?Vampire
08/15/2022, 7:40 PMpoohbar
08/15/2022, 7:42 PMObject obj = "hello";
if (obj instanceof String string) {
// work with 'string'
} else if (obj instanceof Integer integer) {
// work with 'integer'
} else {
// throw
}Joffrey
08/15/2022, 7:45 PMRobert Williams
08/15/2022, 7:47 PMwhen (obj) {
is String -> handleString(obj)
}
...
private fun handleString(newName: String) {
// work with 'newName`Vampire
08/15/2022, 7:51 PMiNumber to signal in the name it is an int. 🤢
Otherwise as the others said, give the overall variable a proper name that is handy in all branches or extract methods.poohbar
08/15/2022, 7:54 PMstringOrInteger. I think we could also just say Java’s feature is more convenient in this specific use case.Vampire
08/15/2022, 7:59 PMstringOrInteger an equally bad name as iNumber.
The types should not be part of the name, the name should express semantics.
For example input or argument or parameter or whether matches your case.
That's what Joffrey meant when he said that your example is too generic and not a sane real-world use-case. 🙂Ruckus
08/15/2022, 7:59 PMstringOrInteger is usually a pretty terrible name. It gives you no semantic meaning as to what the value is meant to represent (unless you are effectively designing your own type system, but even then, I have a hard time seeing how a type of "string or integer" is remotely useful).sreich
08/15/2022, 8:03 PM.let { stupidName -> }PoisonedYouth
08/15/2022, 8:04 PMpoohbar
08/16/2022, 4:25 PMinput or value.
Even then, it is just nicer in Java where I can start by calling it a value and in the smart casted branches I can call it numberValue or textValue.
If you still don’t see how useful this feature can be then I don’t know what else to say. I am a bit disappointed at how “radically” you folks had to express your “correct” opinion but oh well.poohbar
08/16/2022, 4:26 PMVampire
08/16/2022, 4:32 PM.let or using helper methods where you can freely use different names.poohbar
08/16/2022, 4:39 PMyou can say that and we can all disagree to thatI think saying things like this is pretty confrontational. You are somehow implying that everyone except me agrees with you which I doubt. But whatever. Not going to waste mine or anybody else’s time on this further.
Vampire
08/16/2022, 4:43 PMI think saying things like this is pretty confrontational. You are somehow implying that everyone except me agrees with you which I doubt.I'm not at all implying anything similar at all. I said "we can", not "we do". You were the one saying we all don't see a use-case where it is helpful and attacked us for having a different opinion than you. But yeah, let's stop here. 🙂
Ruckus
08/16/2022, 4:50 PMpoohbar
08/16/2022, 4:51 PMpoohbar
08/16/2022, 4:52 PMpoohbar
08/16/2022, 4:52 PMChris Lee
08/16/2022, 4:53 PMRuckus
08/16/2022, 4:53 PMpoohbar
08/16/2022, 4:54 PMlet are good solutions. 👍Vampire
08/16/2022, 4:55 PMI am asking how can I do something and the answer is that I don’t need to. Why not just say that it is not possible?That was implied. I'm sorry if that was not obvious.
Ruckus
08/16/2022, 4:55 PMif (obj is String) {
val myString: String = obj
}
But is isn't clear what your end goal is here. Do you just want another variable? Use val or let. Do you want Java syntax? Not possible (at least not currently). Do you just want elegant usage? No need to do anything, as smart cast covers that very elegantly. And that can go on and on depending on so many things.Ruckus
08/16/2022, 5:06 PMAny sort of generic value processing where values can be of arbitrary types can benefit from this renaming.This is where an actual use case could be helpful. In my experience, I haven't seen such a situation where starting with a better name wasn't a far better option. Of course, neither I nor anyone else here has seen every use case, so a real life example could be very illuminating, as we are clearly having difficulty imagining a case where your claim is true. In your second example, to me it still feels like this issue is that
obj was the wrong name to start with, as it is again a description of the type, not a semantic description of what the value represents.Ruckus
08/16/2022, 5:08 PM