This is emphatically not about Scala. Nor is pattern matching unique to
Scala either. It's standard in most FP languages, and even Java offers a
very rudimentary form through switch statements and the exception handling
syntax.
Let's pretend that Java could handle this as I propose, using a mash-up of
`switch` and exception handling syntax, how might it look? (assuming I
retain commonplace Java idioms as much as possible)
match(fileOpen(fileName)) {
case(FileNotFound status) { ... }
case(IllegalFilename status) { ... }
case(FileOpened fo) {
FileHandle handle = fo.getHandle();
doSomethingWithFile(handle);
moreFileOps(handle);
yetSomethingElse(handle);
.
.
keepGoing(handle);
.
.
promiseThisIsTheLastOne(handle);
}
}
//if there were other subclasses of FileOpenStatus, the compiler would
warn you at this point
//other unanticipated problems can still throw an unchecked exception
This is, of course, just a proof of concept. There are clearly improvements
to be had here, but at least it demonstrates the basic idea: A way to
represent and handle divergent (yet expected) return types without recourse
to explicit casting or checked exceptions.
On 23 September 2010 00:26, Miroslav Pokorny <[email protected]>wrote:
> It seems to me that Kevin always unconditionally says some construct unique
> to Scala is better than a Java equivalent even giving examples and
> reasonings that are for all intents and purposes identical in form,
> functionality and readability... The try catch example for opening a file is
> perfect example.
>
> Kevin please give examples with real differences!!!
>
> --
> You received this message because you are subscribed to the Google Groups
> "The Java Posse" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<javaposse%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/javaposse?hl=en.
>
>
--
Kevin Wright
mail / gtalk / msn : [email protected]
pulse / skype: kev.lee.wright
twitter: @thecoda
--
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en.