On Wed, Aug 19, 2009 at 7:36 AM, Ben Schulz <ya...@gmx.net> wrote: > > > don't feel like you gained much in your sample, you could just replace > "String | Integer" with Object and add a "default: throw new > AssertionError();". Obviously you gained type safety,
Isn't that kinda the point of a type system? :-) I've pushed the constraint that "argument" must be either an Integer or a String out of the documentation where only humans can get at it and into the type system where both humans and tools can understand it. > but it would be > much more useful if unions were structural: > > public static void closeSilently(InputStream|OutputStream|Reader| > Writer|Channel preCloseable) { > try { > preCloseable.close(); > } catch(IOException e) {} > } > Agreed, that would be more useful than only having type dispatch. But union types can join types that share virtually no common structure, so type dispatch is a must to make union typing useful while computing a common structural type is "merely" very nice for cutting down on code repetition. In a way you can see this stuff as generalizing what Java already does with checked exceptions and catch blocks. Several catch blocks in a series form a type dispatch mechanism. It would be nice if unions of exception types could be caught and dealt with using common code, but the exception system would be unusable if you couldn't dispatch on individual exception types. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javaposse@googlegroups.com To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/javaposse?hl=en -~----------~----~----~----~------~----~------~--~---