Hacker News new | ask | show | jobs
by cestith 3352 days ago
How about...

    instanceOf x is? {
        Int { System.out.println("It's an Int"); }
        String { System.out.println("Hello, " + x); }
        _default { System.out.println("This type is not explicitly named here."); }
    }

And if you don't want the type, the value of any other method, function, or attribute could be checked by "is?" or whatever syntax token you want there.

Further tests could be saved for within those blocks to save complexity.

What's odd though is that in Java this particular example seems to want multi-method. Testing the type explicitly and acting on it is more akin to duck-typed language programming. You see this pattern pretty often in Perl for example. If I want to write in Perl, I typically reach for Perl.

1 comments

It's not just the types/instanceof operator though

They're talking about making the Pattern matcher match more complex patterns.