Typedefs

Lawrence Kesteloot lk at teamten.com
Mon Jan 25 16:32:17 PST 2010


On Mon, Jan 25, 2010 at 4:09 PM, Neal Gafter <neal at gafter.com> wrote:
> could either correspond to the language syntax ##T(X)(throws Y)(Z)
> which is a function that takes a Z and returns a #T(X)(Y)

Is anyone else here concerned that the lack of typedefs in Java,
combined with lambda and generics, will make type declarations hard to
read? Already I'm annoyed to read (e.g., from Spring JDBC):

    List<Map<String,Object>> userList;

and I'd rather see:

    typedef Map<String,Object> User;
    typedef List<User> UserList;
    UserList userList;

It feels like lambdas are going to complicate this terribly, e.g., a
function that filters a user list:

    #List<Map<String,Object>>(List<Map<String,Object>>,String)
ageFilter = #(List<Map<String,Object>> userList, String filter) { ...
};

could be better written as:

    typedef #UserList(UserList, String) UserFilter;
    UserFilter ageFilter = ...;

(Yes I know filtering wouldn't be decomposed this way.) A benefit of
this would be the ability to attach a javadoc to the typedef. I know
that Neal's example wasn't meant to be actual code, but if it were, a
typedef for the returned function type would help the programmer
disambiguate:

    #UserFilter(FilterType) userFilterFactory = #(FilterType type) {
return ...; };

instead of:

    ##List<Map<String,Object>>(List<Map<String,Object>>,String)(FilterType)
userFilterFactory = #(FilterType type) { return ...; };

Is there some deep type-theoretical reason why typedefs are difficult
to add to a language? Does it come down to the question of whether two
identical types are assignable to each other?

Lawrence


More information about the lambda-dev mailing list