News for hackers. Stuff that matters.
Re:Pattern Matching for Java by mark242 on Wednesday April 19, 2017 @06:59PM (#14152532)
I mean, reading this article is basically a giant advertisement for Scala and its powerful pattern matching.

  functionThatReturnsAnyVal match {
    case i: Int if i > 2 => System.out.println("Greater than 2")
    case i: Int => System.out.println("Less than or equal to 2")
    case s: String => System.out.println("Hello, " + s)
    case _ =>
  }
Re:Pattern Matching for Java by taeric on Wednesday April 19, 2017 @07:05PM (#14152572)
This still seems a terrible example just to try and avoid naming an object. Isn't the comparison to:

    Object o = functionThatReturnsAnyVal();
    if (o instanceof Integer) {
        Integer i = (Integer) o;
        if (i > 2) {
           System.out.println("Greater than 2.");
        } else {
           System.out.println("Less than or equal to 2.");
        }
     } else if (o instanceof String) {
        String s = (String) s;
        System.out.println("Hello, " + s);
     }
I get that the case syntax is kinda nice, but this particular example just doesn't seem to get there for me. Roughly half the lines, which is good. None of them hard to reason about. Which makes it a wash.

Or is the comparison to something else?

Re:Pattern Matching for Java by emef on Wednesday April 19, 2017 @07:22PM (#14152675)
> I get that the case syntax is kinda nice, but this particular example just doesn't seem to get there for me. Roughly half the lines, which is good. None of them hard to reason about. Which makes it a wash.

I suppose it's a matter of opinion, but to me the comparison of these two snippets is nowhere near "a wash". The readability of the former is leaps and bounds ahead of the java version, and it will only be more apparent as the example grows in complexity.

Re:Pattern Matching for Java by taeric on Wednesday April 19, 2017 @08:03PM (#14152898)
As either example grows in complexity, they both wash down to a check and a call to another method to do the heavy lifting. In that case, the length of the methods will not grow apart from each other heavily.

And let me be clear. My gripe is only with the example. Not the idea. I happen to like pattern matching, but have never used it like those. Usually I use it to tease apart data by parts. (though, to be fair, I have found I use it less than I thought I did. Not sure why...)

Re:Pattern Matching for Java by mark242 on Wednesday April 19, 2017 @10:31PM (#14153498)
In reality my function would rarely exist in the Scala world since having a weakly-typed return like that is cringeworthy to say the least. But it's better to see this in action with Scala options.

  val myOptionalVar: Option[String] = functionThatReturnsOption()

  myOptionalVar match {
    case Some(str) if str.length > 10 => System.out.println("This is a long string.")
    case Some(str) => System.out.println("This is a short string.")
    case None => System.out.println("This code would be fifteen lines of null checking in Java.")
  }
Re:Pattern Matching for Java by pvorb on Thursday April 20, 2017 @03:15AM (#14154589)
With Java 8 having Optional and Lambdas, this is no longer the case:

  final Optional<String> myOptionalVar = functionThatReturnsOptional();
  
  final String output = myOptionalVar
    .map(str -> str.length() > 10
      ? "This is a long string."
      : "This is a short string.")
    .orElse("This code would be fifteen lines of null checking in Java.");
  
  System.out.println(output);
Re:Pattern Matching for Java by tylerpachal on Thursday April 20, 2017 @12:48PM (#14158361)
I think this is less-rare than you think. In my Scala code, I have a lot of matching on weakly-typed values which come from parsing JSON objects (where I also deal with a lot of optional values).
Re:Pattern Matching for Java by chairmanwow on Wednesday April 19, 2017 @10:03PM (#14153397)
I think the prettier syntax actually really helps write code faster. A similar form of syntactic sugar appeared with lambda expressions. Instead of needing to explicitly subclass and write out the overriding method, you can just create an anonymous function.

It's way less code and it makes the resulting code easier to reason about. The code's intent is more elegantly conveyed.

Re:Pattern Matching for Java by nightmunnas on Wednesday April 19, 2017 @07:11PM (#14152606)
(String) o;

:)

Re:Pattern Matching for Java by taeric on Wednesday April 19, 2017 @07:16PM (#14152633)
Fair. :) I was much more worried about if I had the required spaces at the beginning of the line, than if I wrote this correctly. Still a static error, though, that the compiler would catch.
Re:Pattern Matching for Java by sgift on Thursday April 20, 2017 @02:31AM (#14154426)
> reading this article is basically a giant advertisement for Scala and its powerful pattern matching.

Javas strength has always been to take the good parts of its competitors after they've checked out in production (and not just "wouldn't this be a great idea ..?") and implement them. It will probably never be ahead of the curve due to this, but at least it is remarkably free of "looks good in theory, useless in reality"-features.

Re:Pattern Matching for Java by RandomBK on Thursday April 20, 2017 @02:47AM (#14154484)
In general, I agree with all but your last point. There are many things in Java that were good ideas at the time of introduction, but turned out horrible (serializable, finalize, etc.)
Re:Pattern Matching for Java by sgift on Thursday April 20, 2017 @03:01AM (#14154532)
Fair enough. I think both of your examples exist since JDK 1.0 (or 1.1), though I'd have to look to be sure. I think they've wised up after that.
Re:Pattern Matching for Java by brogrammer_1 on Thursday April 20, 2017 @05:04AM (#14155075)
Type erasure for generics...
Re:Pattern Matching for Java by pron on Thursday April 20, 2017 @05:08AM (#14155098)
Wrong. Type erasure for generics is one of Java's luckiest breaks. At worst, it imposes a tiny inconvenience, but in exchange this is what allowed Java not to bake a variance model into the VM and libraries, and this is precisely what allows languages with different variance models -- like Java, Kotlin and Clojure -- to both share code and actual data objects with no runtime conversions. This is something that can't happen on .NET (see, e.g. how clumsily their Python implementation interacts with generics). This was a huge win for almost no cost.
Re:Pattern Matching for Java by ddispaltro on Thursday April 20, 2017 @08:33AM (#14156048)
I completely agree, what a blessing in disguise. Scala wouldn't be possible without it.
Re:Pattern Matching for Java by tome on Friday April 28, 2017 @05:44AM (#14218082)
It was designed in part by Phil Wadler, so it's not surprising it's a success.
Re:Pattern Matching for Java by hota_mazi on Thursday April 20, 2017 @12:04AM (#14153845)
You mean it's a giant advertisement for ML and its powerful pattern matching?