Joint types inconsistency: Suggesting we use & instead of | for exceptions.

Peter Levart peter.levart at marand.si
Tue Feb 2 06:27:28 PST 2010


On Tuesday 02 February 2010 14:53:22 Reinier Zwitserloot wrote:
> In generics, there's a concept that's fairly close to a joint type, which
> involves the ampersand (&) character:
> 
> public class Foo<T extends List & Serializable> {}
> 
> However, in the proposed (draft v0.1) closure syntax, | is used to make a
> "joint type" for exceptions:
> 
> #int(int, int)(throws IOException|SQLException) x;
> 
> While the exception list there isn't really a single (joint) type, there are
> some conceptual similarities, and it makes a lot more sense to say that a
> method throws both IOException and SQLException, than to say it throws
> either IOException or SQLException (checked exceptions are interesting on
> the type level to track what you need to catch, and if a method can throw
> either, then you need to catch both in order to satisfy javac, hence, it
> makes more sense to consider this a joint type instead of a disjoint type).

If the method throws an exception and you catch it and assign to a variable. What type does that variable have to be?

If variable is of type "IOException|SQLException" that means it can hold a reference to an object that is either an IOException or a SQLException (or both, but such object is impossible in java). This is different from variable of type Exception which can hold objects of other types too.

If variable is of type "Number&Comparable" that means it can hold an object which is a Number and a Comparable at the same time. It can not hold an object which is a Number but isn't a Comparable and it can not hold an object that is a Comparable but isn't a Number.

> NB: Is there any reason why it can't mirror method declarations and use
> commas?

If throws declaration in function type is not enclosed in parens it is ambiguous:

FunctionType:
    '#' ReturnType '(' ParameterTypes_opt ')' Throws_opt

Throws:
    throws ExceptionTypes

ExceptionTypes:
  ExceptionType
  ExceptionType ',' ExceptionTypes


For example:

#int( #void() throws IOException, SQLException )

Is this a function type returning int and taking 2 parameters of type "#void() throws IOException" and "SQLException" or
is this a function type returning int ant taking 1 parameter of type "#void() throws IOException, SQLException"


Regards, Peter


More information about the lambda-dev mailing list