The Artima Developer Community
Sponsored Link

Java Community News
Elliotte Rusty Harold on Type Inference for Java, and on Moving On

36 replies on 3 pages. Most recent reply: Apr 27, 2007 7:26 AM by Isaac Gouy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 36 replies on 3 pages [ « | 1 2 3 ]
Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 25, 2007 6:25 PM
Reply to this message Reply
Advertisement
Abraham Tehrani said, Given the choice of taking Josh Bloch word and yours, no offense, but I will take Josh's word anyday.

Given a choice, I would rather think for myself and decide, not take anybosy's word for anything. This slavish adherence to "authority" is detrimental to ones well-being and peace of mind. No offence, Abraham.

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 25, 2007 6:30 PM
Reply to this message Reply
Abraham Teherani said,

public ArrayList<Customers> getCustomers();

public List<Customers> getCustomers();

The latter is preferred for (hopefully) obvious reasons.


Actually, I would prefer
 

public Customer[] getCustomers();


I think that the customer array is more natural than list of customers, since we can use customers rather than customers.get(i).

To each his own.

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 25, 2007 6:33 PM
Reply to this message Reply
Max Lybbert wrote

> Given a particular literal (say, {4, "hello"}) how can I
> infer the correct class for that object?

Well in C# ...
   var a = new {id=4,message="hello"};
   Console.WriteLine(a.GetType());

<>f__AnonymousType0`2[System.Int32,System.String]

Daniel Jimenez

Posts: 40
Nickname: djimenez
Registered: Dec, 2004

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 26, 2007 6:47 AM
Reply to this message Reply
Me:
>> and it's not my experience that invoking
>> HashMap-specific methods is rare.


Carson:
> Hashmap's methods form a proper subset of TreeMap's.
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.h
> tml
>
> http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeMap.h
> tml
>
> Using a HashMap-specific method would be a trick.
>
> Cheers,
> Carson

*wipes egg off face (original reply) with napkin (s/HashMap/Hashtable/g)*

Thanks!

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 26, 2007 10:32 AM
Reply to this message Reply
> public Customer[] getCustomers();

> I think that the customer array is more natural than
> list of customers

List offers a lot of extra capabilities over an array. Like add, remove, immutability, synchronization.

I'd say that one should prefer List to arrays but you want to think for yourself. :-)

Max Lybbert

Posts: 314
Nickname: mlybbert
Registered: Apr, 2005

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 26, 2007 8:33 PM
Reply to this message Reply
/* [Me] Given a particular literal (say, {4, "hello"}) how can I infer the correct class for that object?

[Response] Well in C# ...
    var a = new {id=4,message="hello"};
    Console.WriteLine(a.GetType());

<>f__AnonymousType0`2[System.Int32,System.String]

*/

If you're going to create a new type for every literal, then the answer is trivial. If you're going to try to choose the type from those already defined at the literal's point of instantiation, then you've got a mess on your hands. Especially in a relatively common case of multiple classes having the same data members but different methods/behaviors.

The only answer I can see is to annotate the correct type, which means "don't use type inference in that case."

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Elliotte Rusty Harold on Type Inference for Java, and on Moving On Posted: Apr 27, 2007 7:26 AM
Reply to this message Reply
Max Lybbert wrote
> If you're going to create a new type for every literal,
> then the answer is trivial.

"trivial" seems like a good thing in this context.

> If you're going to try to
> choose the type from those already defined at the
> literal's point of instantiation, then you've got a mess
> on your hands. Especially in a relatively common case of
> multiple classes having the same data members but
> different methods/behaviors.
>
> The only answer I can see is to annotate the correct type,
> which means "don't use type inference in that case."

Given the trivial creation of a new type maybe the question shifts to what can we do with this new type - can we treat is a subtype of something else? which things?

Flat View: This topic has 36 replies on 3 pages [ « | 1  2  3 ]
Topic: Cay Horstmann on the Meaning of Return Previous Topic   Next Topic Topic: Scott Ambler on Agile Myths


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us