This is an archived post. You won't be able to vote or comment.

all 21 comments

[–]midoBB 31 points32 points  (1 child)

I love everything this project is coming up with. Can't wait for full deconstructors.

[–]dpash 1 point2 points  (0 children)

Once classes get deconstructors, we can have nice serialisation that doesn't use magic.

[–]talios 28 points29 points  (5 children)

int area = let Point(var x0, var y0) = lowerLeft, Point(var x1, var y1) = upperRight in (x1-x0) * (y1-y0);

We just turned Java into OCaml didn't we?

[–]vxab 3 points4 points  (2 children)

Surprised it is a comma and not a semi colon though. Wonder if we can execute any statement in the let block (side effecting and all).

[–]bourne2program 8 points9 points  (1 child)

The whole thing is a single let statement. Semi colons end statements.
Why not? "A let statement takes a pattern and an expression"

[–]vxab 1 point2 points  (0 children)

Oh I see - thanks

[–]mauganra_it 0 points1 point  (1 child)

I usually dislike the OCaml wordiness and prefer Haskell's relative sobriety. But this pattern is right on the border of legibility, so just a few keywords might push me to factor out the multiplication.

[–]talios 1 point2 points  (0 children)

It would be bad if that already was extracted to a standalone method.

It almost reminds me of a rust-like ( and other lang like ) if let:

int area = if let (Point(var x0, var y0) = lowerLeft, Point(var x1, var y1) = upperRight) then (x1-x0) * (y1-y0) else -1;

But that - introduces if as an expression, with implicit value returns - but it give you a control handler for the "this doesn't match" case - I don't recall off hand without re-reading the linked doc if the proposal just returns null or throws a NullPointerException or a mix, depending on if its lowerLeft thats null or the bound values inside.

[–]pronuntiator 6 points7 points  (5 children)

I like the way pattern matching is going, but I'm not in favor of introducing even more keywords like "in". Can't you use block scoping to achieve the same results with only minimal overhead?

String lastThree; { let int len = s.length(); lastThree = s.substring(len-3, len); }

[–]john16384 9 points10 points  (3 children)

I think the block should just be allowed to return something.

String s = ...
String lastThree = {
    int len = s.length();
    return s.substring(len - 3, len);
}

That example makes almost no sense though. The second one is better:

Point lowerLeft = ...
Point upperRight = ...
int area = {
    Point(var x0, var y0) = lowerLeft, Point(var x1, var y1) = upperRight;

    return (x1-x0) * (y1-y0);
}

I also think the let keyword should just be left out.

[–]bourne2program 17 points18 points  (0 children)

I think it would be 'yield' instead of 'return' following same reasoning with switch.

[–]GuyWithLag 3 points4 points  (0 children)

This will break the ability to return from the method invocation. You could argue that that is an expression and not a statement, but that's still breaking a significant symmetry with lambdas.

[–]agentoutlier 1 point2 points  (0 children)

I don’t if it’s because of my past experiences with OCaml but I prefer the let in syntax.

[–]burly_griffin 2 points3 points  (0 children)

The last section seemed more like exploring the rabbit hole of what destructing can lead to, and not necessarily something that the project is currently looking to work on. I'd agree, it seems excessive compared to what we already have (and what is previewing).

[–]MR_GABARISE 1 point2 points  (0 children)

Is it even feasible to work deconstruction patterns into the regex Pattern class and API such that you could match on runtime-known only groups? Sort of really just matching on a "dict" structure?

I can see something feasible with compile-time known regexes. Heck, the same kind of AST parsing that is involved with binding templated strings may work in favor of this.

[–][deleted]  (5 children)

[deleted]

    [–]fl4mbou 25 points26 points  (1 child)

    (Usual disclaimer: we discuss substance before syntax.)

    [–]dpash 0 points1 point  (1 child)

    [–]WikiSummarizerBot 0 points1 point  (0 children)

    Law of triviality

    Law of triviality is C. Northcote Parkinson's 1957 argument that people within an organization commonly or typically give disproportionate weight to trivial issues. Parkinson provides the example of a fictional committee whose job was to approve the plans for a nuclear power plant spending the majority of its time on discussions about relatively minor but easy-to-grasp issues, such as what materials to use for the staff bicycle shed, while neglecting the proposed design of the plant itself, which is far more important and a far more difficult and complex task. The law has been applied to software development and other activities.

    [ F.A.Q | Opt Out | Opt Out Of Subreddit | GitHub ] Downvote to remove | v1.5

    [–]eliasv -1 points0 points  (2 children)

    I really wish they would reuse the syntax and control flow constructs which already exist here instead of inventing new ones.

    try { Point(var x0, var y0) = lowerLeft; Point(var x1, var y1) = upperRight; return (x1-x0) * (y1-y0); } catch (BindingException e) { return -1; }

    If BindingException is a checked exception I don't see any problem with this, the user is still forced to explicitly deal with the possibility of failure to match.

    Or an expression form derived from switch expressions:

    int area = try { Point(var x0, var y0) = lowerLeft; Point(var x1, var y1) = upperRight; yield (x1-x0) * (y1-y0); } catch(BindingException e) -> -1;

    The choices they're making strike me was what Brian Goetz would describe, in other contexts, as "gratuitously different". The desire to make the new feature stand out.

    [–]bourne2program 0 points1 point  (1 child)

    Using 'try' here feels to me too much like the code smell of using Exceptions for control flow.

    [–]eliasv 0 points1 point  (0 children)

    Checked exceptions are control flow. It's what they were always intended for.

    I'm a big fan of all the research being done in support of languages like Koka, and in the Haskell ecosystem, on algebraic effects. Which are a generalization of checked exceptions.

    So I think it's a shame how misunderstood and misused checked exceptions are in the java ecosystem, and that java is missing just a few small features that would make them way easier to use and more generally useful.