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

Reinier Zwitserloot reinier at zwitserloot.com
Tue Feb 2 06:45:15 PST 2010


Well, without parens it would indeed be ambiguous, but as far as I can tell
in the proposal, they are mandatory. Interesting dilemma - making parens
mandatory is somewhat odd as they certainly aren't in method declarations
and it makes the closure syntax rather paren heavy, but on the other hand
without parens you're forced to use | or & instead of the commas that throws
clauses on method declarations use.

So, the idea, then, is that parens are optional, hence the bar instead of a
comma?


Point taken that from the perspective of those _catching_ the exception,
exception types feel more 'or' than 'and', but I stick to the notion that
exception types feels more 'and' like to me when thinking about what your
method can throw (I declare at this point that I may throw both type A and
type B in addition to RuntimeException and Error), and pipe is still
somewhat unreadable.

But, my original concerns about consistency have mostly been assuaged.

My personal preference is leaning towards mandatory parens and commas. The
reason for this:

If a user misunderstands and gets this wrong*, then with the mandatory
parens all the common mistakes will be trivially caught by the parser, and
thus result in a clear and immediate error message - throws is a keyword and
is trivially illegal in most places, except in a parenthesized throws clause
on a function type. The commas can then be kept which is consistent with how
'throws' clauses have to be written where all java programmers know them
from: method declarations.

*) I rate this as likely; throwing multiple exception types is not something
you type every day, and I can easily imagine hordes of java programmers will
not be using closures all that often either, meaning they may only write a
multi-throws type once a quarter or less, easily rare enough to forget the
exact syntax.

--Reinier Zwitserloot

Need to receive donations via the web?
Check https://tipit.to/



On Tue, Feb 2, 2010 at 3:27 PM, Peter Levart <peter.levart at marand.si> wrote:

> 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