News for hackers. Stuff that matters.
Re:Deprecating: java.util.Optional.get()? by lmm on Thursday April 28, 2016 @11:57AM (#11589798)
I feel like that's just a special case of casting and so should be handled by casting. You can always cast Optional to Some, can't you? It's verbose and dangerous-looking but it should be verbose and dangerous-looking in my book.
Re:Deprecating: java.util.Optional.get()? by steveklabnik on Thursday April 28, 2016 @12:06PM (#11589861)
It depends on the language. In a language like Rust, direct casts mean "reinterpret these bits", and the two are represented differently, so that wouldn't work. In a less low-level language, then sure, maybe a cast would work for this case. I'm not sure that it's any more verbose or scary looking, though.
Re:Deprecating: java.util.Optional.get()? by lmm on Thursday April 28, 2016 @12:25PM (#11589982)
> In a language like Rust, direct casts mean "reinterpret these bits", and the two are represented differently, so that wouldn't work.

What's the representation of an optional then? Doesn't make use of some generic concept like subclasses or unions? Surely you have a generic syntax for "assume this union is this particular member" or "assume this base class is this particular subclass" or the like?

> I'm not sure that it's any more verbose or scary looking, though.

I guess the point is that I want all cast-like operations to look alike (or at least to have a way to consistently distinguish them from ordinary methods) so that I can flag them all up consistently in code reviews, linters etc.

Re:Deprecating: java.util.Optional.get()? by steveklabnik on Thursday April 28, 2016 @12:34PM (#11590062)
It's a tagged union. Sometimes the tag can be elided, sometimes not. If you did a direct cast, you'd lose the tag. More specifically though, layout is undefined, so you don't know if the tag is first or last, strictly speaking.

Lints for unwrap already exist :)

Re:Deprecating: java.util.Optional.get()? by lmm on Thursday April 28, 2016 @01:35PM (#11590556)
> It's a tagged union. Sometimes the tag can be elided, sometimes not. If you did a direct cast, you'd lose the tag. More specifically though, layout is undefined, so you don't know if the tag is first or last, strictly speaking.

Do you not have a generic syntax for unwrapping a tagged union, forcibly assuming that it's one particular component? That seems like something users would want.

Re:Deprecating: java.util.Optional.get()? by steveklabnik on Thursday April 28, 2016 @01:56PM (#11590697)
That is exactly what unwrap() is...

(More specifically, it's a method which uses `match` internally to do this)

Re:Deprecating: java.util.Optional.get()? by lmm on Thursday April 28, 2016 @02:21PM (#11590884)
Ok, so I'm advocating forcing users to always use match directly if they want to do that. That helps ensure unsafe code looks unsafe.
Re:Deprecating: java.util.Optional.get()? by tatterdemalion on Thursday April 28, 2016 @02:48PM (#11591071)
Maybe your issue is that the `get` method in Java sounds pretty innocuous, but in Rust the `unwrap` method is notorious. Every Rust user knows that it can panic. Requiring a match would increase verbosity and decrease clarity.
Re:Deprecating: java.util.Optional.get()? by pcwalton on Thursday April 28, 2016 @01:59PM (#11590715)
Why add a language feature when a library feature does the same thing?
Re:Deprecating: java.util.Optional.get()? by ben0x539 on Thursday April 28, 2016 @02:04PM (#11590759)
Hey, it worked for try!()/?