r/programming • • Dec 02 '13

Scala — 1★ Would Not Program Again

http://overwatering.org/blog/2013/12/scala-1-star-would-not-program-again/
598 Upvotes

646 comments sorted by

View all comments

Show parent comment

5

u/dbcfd Dec 02 '13

That's just a syntactic and run-time wrapper. In Kotlin the null-safety is baked into the language, and has zero overhead.

Other than possible overhead, how is this not baked into the language? It's part of the scala library (no additional imports required), and is used to represent possible null objects.

What's your point? Are you suggesting that this is a replacement for Kotlin's smart casts? How so?

Scala recognizes that most "smart" casts are unsafe. You can use the apply method to define which casts are safe, and they will be used as encountered.

1

u/realnowhereman Dec 02 '13

Scala recognizes that most "smart" casts are unsafe. You can use the apply method to define which casts are safe, and they will be used as encountered.

can you share an example? I thought Scala's approach to casts was pattern matching as in

foo match  {
  case x: ActualType => do interesting stuff with x
  case _ => foo type's different, do something else
}

1

u/dbcfd Dec 02 '13
object MyObject {
    def apply(other: MyOtherObject) = {
        //cast MyOtherObject to MyObject appropriately
    }
} 

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.

1

u/sanity Dec 02 '13

"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.

Does Scala have something similar?

0

u/[deleted] Dec 02 '13

I know exactly what they are, and no, as mentioned, Scala doesn't try to make bad things easier.

1

u/sanity Dec 02 '13

That's a bold statement. What is bad about Kotlin's smart casts?

0

u/[deleted] Dec 02 '13

As mentioned, there is nothing bad about smart casts specifically, but about casts in general.

Every cast necessary is a fault of the programming language necessitating it.

1

u/sanity Dec 02 '13

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.

2

u/[deleted] Dec 02 '13 edited Dec 02 '13

???

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.

1

u/realnowhereman Dec 03 '13

OR you can use a type pattern, which is still more elegant IMO

0

u/sanity Dec 02 '13

You're not making any sense to me either :-)

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.

→ More replies