Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Comparing types of generic variables that are not instantiated
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Comparing types of generic variables that are not instantiated




In the third case, you are constructing a pair where f and s are both null. Thus the line


f2.compareTo(s);

is throwing a NullPointerException.

The conceptual problem is that since Java implements generics via type erasure (for good or ill), the types supplied in a parameterization (e.g. Pair<Integer, Integer>) only have meaning at compile time. At run time, the actual types of the local variables have been reduced to their least common denominator, probably Comparable in this case.

You can, as you are doing, compare the RUN TIME types of the values actually stored in the local variables, but information as to what the original concrete type parameters were does not survive to runtime.

-wilhelm

On May 24, 2007, at 3:22 AM, Kolbjørn Aambø wrote:

The code below give the following output where the third line is not what I wanted:

[Session started at 2007-05-24 11:54:51 +0200.]
Harmonic variable types			// ok
NON Harmonic variable types		// ok
NON Harmonic variable types		// WRONG
Harmonic variable types			// ok

Is there a way to correct this problem without getting compilation errors?

I would like to parameterize a class further after instansiating it based on whether the generic types are the same or not but without taking into consideration any instantiated variables that are often inserted using put(...) quite another place in the code.

I have perused pair as an example because the code here is particularly small.

I really hope there is something I have not understood

Kolbjørn Aambø

//
//  tryPair.java
//  tryPair
//
//  Created by K Aa on 5/24/07.
//
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.util.*;

public class tryPair {

public static class Pair<First extends Comparable<First>,
Second extends Comparable<Second>>
implements Comparable <Pair<First, Second>> {
public First f = null;
public Second s = null;

public Pair(){} // -------- This is used for the interesting part -------


public Pair (First f, Second s) {
if (f == null)
throw new NullPointerException("first is null");
if (s == null)
throw new NullPointerException("Second is null");
this.f = f;
this.s = s;
}

public Pair <First, Second> put (First f, Second s) {
if (f == null)
throw new NullPointerException("first is null");
if (s == null)
throw new NullPointerException("Second is null");
this.f = f;
this.s = s;
return this;
}

/*
* To detect if First and Second has the SAME TYPE by sampling one pair.
* The problem is that you have to have an actual Pair to check
* before you can say that a Pair is homogenous i.e. that
* First == Second. I would like to simply test directly inside
* the Pair instance.... even if there where no instance variables...
*/
public boolean harmonicTypes(){
try {
Comparable<Second> f2 = (Comparable<Second>) f;
f2.compareTo(s);
} catch (Throwable trow){
return false;
}
return true;
}


	  public int compareTo(Pair<First, Second> o) {
		int fCompare = f.compareTo(o.f);
		return fCompare == 0 ? s.compareTo(o.s) : fCompare;
	  }

	  public boolean equals(Object o) {
		if (o instanceof Pair) {
		  Pair<?,?> p = (Pair<?, ?>) o;
		  return this.f.equals(p.f) && this.s.equals(p.s);
		}
		return false;
	  }

	  public String toString(){return f.toString()+"@"+s.toString();}

	  public int hashCode(){
		return f.hashCode() +  31 * s.hashCode();
	  }
	}

public static void main (String args[]) {
// insert code here...
Pair<Integer, Integer> harmonic = new Pair<Integer, Integer>(1,2);
if (harmonic.harmonicTypes()) System.out.println("Harmonic variable types");
else System.out.println("NON Harmonic variable types");

Pair<Integer, String> nonharmonic = new Pair<Integer, String> (1,"2");
if (nonharmonic.harmonicTypes()) System.out.println("Harmonic variable types");
else System.out.println("NON Harmonic variable types");

// DONT RETURN THE RESULT I WANT, CAN THIS BE FIXED?
Pair<Integer, Integer> interesting = new Pair<Integer, Integer> (); // Only generic types are given
if (interesting.harmonicTypes()) System.out.println("Harmonic variable types");
else System.out.println("NON Harmonic variable types");



// Quite a bit longer down in the code.. interesting.put (1 , 2); } _______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Comparing types of generic variables that are not instantiated (From: Kolbjørn Aambø <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.