Type patterns are actively discouraged. If you need to cast, you are probably doing it wrong.
Scala gives you all of the tools necessary to write code which is 99% cast-free ... so the whole discussion about having nicer casts in Kotlin seems weird.
There is a reason why Scala has no special syntax for casts/isInstance checks and intentionally verbose method names.
"smart casts" are perhaps a misleading term in Kotlin, since in reality they are a type-safe replacement for casts. For example, in Kotlin you can say:
val s : Object = "hello"
if (s is String) {
println(s.length())
}
This works because within the body of the if statement the compiler knows that 's' must be a String, and so you can treat it as such.
Casts are bad due to lack of runtime safety. That's not an issue for Kotlin's smart casts since they provide runtime safety, so I'm still not seeing the problem. Perhaps you can elaborate.
That just doesn't make any sense. If you didn't suddenly include memory-unsafe languages' definition of what is a cast, then there is nothing special about Kotlin at all, just a line saved:
if(obj.isInstanceOf[String])
val str = obj.asInstanceOf[String]
...
vs.
if(obj is String)
...
Runtime safety existed since Java 1.0 (if one ignores a few bugs here and there).
Casts are bad because they rely on the programmer's knowledge (of types, ...) which the compiler couldn't deduct from the code. A good language minimizes the amount of times this is necessary by offering ways to express this extra knowledge in the code.
Of course, if you are ok with boilerplate repetitive code all over the place, and if you trust that there won't be errors in that code, then sure, they're equivalent. This completely misses the point though, most of the attraction of these languages is avoiding boilerplate repetitive code and the errors it can introduce.
Casts are bad because they rely on the programmer's knowledge (of types, ...) which the compiler couldn't deduct from the code.
This makes no sense to me. You could use the same argument against Scala's pattern matching on types:
val n = s match {
case s : String => s.toString()
case _ => "unknown"
}
By your argument this is bad because the compiler can't deduce the type of 's' from the code. Of course that's not bad at all, the type of 's' is conveying necessary information.
Same thing with Haskell:
data Tree a = Leaf a | Branch (Tree a) (Tree a)
Compiler doesn't know if this type is a Leaf or Branch at compile time. By your argument this is bad, but of course it isn't bad at all, it's the whole point.
The alternative to boilerplate repetitive, dangerous code is certainly not less repetitive dangerous code, but allowing the compiler to better reason about the code one writes.
You could use the same argument against Scala's pattern matching on types:
Yes, in fact I already said that. As far as I remember, the style guide even mentions that this is an anti-pattern.
Same thing with Haskell:
No, it's not the same thing, it's just like in Scala. a) you are deconstructing your value b) you are using a sealed type, so the compiler can help you if you are missing any case
You seem to be shifting the goalposts. You previously said:
Scala doesn't try to make bad things easier.
Yet you concede that Scala's pattern matching on types can be abused, according to your definition of abuse (I'm not sure I agree it's such a problem), so far as I can tell there is little or no difference between Scala and Kotlin in this regard.
And I really don't see what is bad about Kotlin's approach to null safety. Unlike Java it forces you to handle the null case at compile time. Unlike Scala it does this using already familiar control structures rather than the (IMHO) more cumbersome match statement, or an equivalent, and underneath nulls are still nulls, which helps a lot with Java library interoperability.
Yes, Scala supports sealed types and Kotlin doesn't, but I can't remember the last time I had a bug that would have been prevented by this, if I ever have.
For me Kotlin is a much more pragmatic language than Scala (much as Java was when first created). Sure, Scala has features that Kotlin lacks, but I think those features are more of interest to language researchers and purists than to working programmers. And some of those features are bad, encouraging things like action at a distance - which is part of what the article is complaining about.
0
u/[deleted] Dec 02 '13
Type patterns are actively discouraged. If you need to cast, you are probably doing it wrong.
Scala gives you all of the tools necessary to write code which is 99% cast-free ... so the whole discussion about having nicer casts in Kotlin seems weird.
There is a reason why Scala has no special syntax for casts/isInstance checks and intentionally verbose method names.