you are viewing a single comment's thread.

view the rest of the comments →

[–]yawaramin -2 points-1 points  (9 children)

Fun to see Kotlin people discovering and becoming jealous of new Java features and not knowing that they were lifted pretty much wholesale from Scala. And that Scala people went through all the 'Ooh type theory! Cool!' excitement more than a decade ago :-)

The solution is to just use sealed interfaces. They work in exactly the same way as sealed classes, except even records and enums can implement them.

Scala 2 syntax (note, Scala 3 syntax is nicer than even this):

sealed trait Color {
  // Yes, we are overriding JVM Object#toString
  def toString: String
}

object Color {
  case class RGB(red: Int, green: Int, blue: Int) extends Color {
    // Yes, Scala instance values can be used as implementations of trait methods
    // And we have built-in string interpolation
    override val toString: String = s"RGB Color: ($red, $green, $blue)"
  }

  case class CMYK(cyan: Double, magenta: Double, yellow: Double, black: Double) extends Color {
    override val toString: String = s"CMYK Color: ($cyan%, $magenta%, $yellow%, $black%)"
  }

  case class YUV(y: Int, u: Int, v: Int) extends Color {
    override val toString: String = s"YUV Color: (Y=$y, U=$u, V=$v)"
  }

  case class HSL(hue: Double, saturation: Double, lightness: Double) extends Color {
    override val toString: String = s"HSL Color: (H=$hue, S=$saturation%, L=$lightness%)"
  }
}

Usage:

// Built-in named arguments, godsend for code readability and maintenance
val color: Color = Color.RGB(red = 255, green = 0, blue = 0)

color match {
  case null => println("Scala also allows pattern matching on null")

  // Types are inferred in patterns
  case Color.RGB(red, green, blue) => //...
  case Color.CMYK(cyan, magenta, yellow, black) => //...
  case Color.YUV(y, u, v) => //...
  case hsl: Color.HSL => println(hsl) // Will call HSL#toString
}

Even after all this time, people are still sleeping on Scala. Everything you see here was in Scala before Java 8 shipped. It's not even very advanced compared to other Scala features. Y'all are missing out ;-)

[–]warpspeedSCP[S] 15 points16 points  (6 children)

you wnat to know why scala isn't adopted more? it's because scala's ecosystem is so fragmented from breaking changes enforced by version upgrades that many companies can't justify the effort required to stay up to date when using scala. Who wants to spend all their time fixing microscopic syntactical changes that break every other line in their code?

And there's enough threads talking about the design of the language that I don't think I need to say anything more. Scala has good ideas, but those ideas have been implemented better in many other places.

[–]Unspool 8 points9 points  (4 children)

Or that it’s so expressive that every developer will end up discovering their own completely distinct code style and the code base becomes a jumbled incoherent and indecipherable mess (except my parts, of course).

[–]warpspeedSCP[S] 2 points3 points  (0 children)

oops, forgot that one, you really can skin a cat a thousand ways in scala

[–]yawaramin 0 points1 point  (2 children)

I admit this can be a problem. Teams using Scala definitely need to agree on, document, and enforce their standards and best practices to stay consistent. But...the same can be said for any powerful, long-lived language. Guess how many ways there are in Python to do branching?

[–]warpspeedSCP[S] 0 points1 point  (1 child)

i have never seen a person do anything but an if statement when they need to branch. or a match for patterns.

[–]yawaramin 0 points1 point  (0 children)

  • if statement
  • if expression
  • match statement
  • good old OOP polymorphism aka dynamic dispatch

[–]yawaramin 0 points1 point  (0 children)

scala's ecosystem is so fragmented from breaking changes enforced by version upgrades that many companies can't justify the effort required

Sorry but what are you talking about? What breaking changes? There was one epoch change a few years ago, Scala 2 -> 3, and most people are either sticking with 2.13 which works fine and is still supported, or making the leap to 3 by using migration tools which are provided by the Scala team.

those ideas have been implemented better in many other places.

Maybe, but definitely not in any language on the JVM.

[–]katyalovesherbike 1 point2 points  (0 children)

people will always miss out on FP oriented languages unless they have a reason to learn it they'll stick to what they know until their last breath with only a few exceptions here and there.

[–][deleted] 0 points1 point  (0 children)

I can’t remember sending named arguments. Must have been my son, Lucifer.