Martin C. Martin | 15 May 15:48

Re: [groovy-dev] Declaring multiple return types (Was: Re: [groovy-dev] I'll give a Groovy presentation at the Jazoon'08)

There are a couple ways to do this already.  Ones is an inner class, 
something like:

class City {
   class CityAndDistance {
     def City city
     def double distance
   }

   CityAndDistance getClosestCity() { ... }
}

But its pretty verbose.  Another is to use Generics:

Pair<City, double> getClosestCity() { .. }

Basically, this boils down to a notation for types that can reference 
other types.  This seems like a straightforward thing, but then you get 
into all the problems that Generics have, like parameterized types.  I 
don't know that there's a good solution for this.

Best,
Martin

Aaron Digulla wrote:
> Quoting "Martin C. Martin" <martin@...>:
> 
>>> It suffices to put the types on parameters only, the return type  
>>> then should be inferable.
>>> (Recursive methods are an exception, but that is another story)
>>
>> By the compiler, or by the user?  How would a user infer the return
>> type of something like this:
>>
>> def getAllConnectingCities(String origin_city)
>>
>> ?
>>
>> From the name, it's probably returning a collection.  But is it a Set,
>> a List, an array, or a hash table?  Or is it returning multiple values
>> wrapped in an Object[], say the collection along with distance of the
>> nearest and furthest cities?
> 
> Hm... there is a feature request lurking here:
> 
>     class City {
>         [City, double distance] getClosestCity() { ... }
>     }
> 
> to declare the types of the two return values (along with the 
> information that the method returns two values and what they mean).
> 
> Regards,
> 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane