Object retval = (Object)bindings.get(var); // why not OK?

M

metaperl

I'm wondering why I cannot simply cast the return result of .get() to
satisfy the compiler expectation that the method return an object.
Instead I have to assign the result of .get() to a variable and then
return that.

package redick;

import java.util.*;
import java.util.Iterator;

public class Environment {
/*
* map from a variable name to an Object - which may be a datum or a
* procedure
*/
public Map<String,Object> bindings = new HashMap<String, Object>();
public Environment parent;

public void put(String var, Object value) {
bindings.put(var, value);
}
public Object get(String var) {

Object retval = bindings.get(var); // cast not enough
/* Cannot comment this section out */
if (retval == null) {
return (Object)null;
} else {
return retval;
}
/* End required section */
}

public String toString() { return bindings.toString(); }

}
 
L

Lew

metaperl said:
I'm wondering why I cannot simply cast the return result of .get() to
satisfy the compiler expectation that the method return an object.
Instead I have to assign the result of .get() to a variable and then
return that.

You never have to upcast, so a cast to Object is never needed.
package redick;

import java.util.*;
import java.util.Iterator;

public class Environment {

Please do not embed TABs in Usenet posts.
/*
* map from a variable name to an Object - which may be a datum or a
* procedure
*/
public Map<String,Object> bindings = new HashMap<String, Object>();
public Environment parent;

public void put(String var, Object value) {
bindings.put(var, value);
}
public Object get(String var) {

Object retval = bindings.get(var); // cast not enough

There is no cast here, and indeed, none is needed.
/* Cannot comment this section out */
if (retval == null) {
return (Object)null;
} else {
return retval;
}
/* End required section */
}

public String toString() { return bindings.toString(); }

}

You could simply "return retval;" or even "return bindings.get(var);"

public Object get(String var)
{
return bindings.get(var);
}

You haven't shown us a complete (i.e., "SSCCE") example of the erroneous code
that gives you trouble, or what the error is. I don't see what your
difficulty could have been.

There are times when cover methods delegate with one-line bodies, but
sometimes that's an indicator that you can use the Map directly instead of
wrapping it.
 
D

Daniel Pitts

I'm wondering why I cannot simply cast the return result of .get() to
satisfy the compiler expectation that the method return an object.
Instead I have to assign the result of .get() to a variable and then
return that.

package redick;

import java.util.*;
import java.util.Iterator;

public class Environment {
/*
* map from a variable name to an Object - which may be a datum or a
* procedure
*/
public Map<String,Object> bindings = new HashMap<String, Object>();
public Environment parent;

public void put(String var, Object value) {
bindings.put(var, value);
}
public Object get(String var) {

Object retval = bindings.get(var); // cast not enough
/* Cannot comment this section out */
if (retval == null) {
return (Object)null;
} else {
return retval;
}
/* End required section */
}

public String toString() { return bindings.toString(); }

}

Works for me, what're your error message?
 
M

Mark Space

metaperl said:
I'm wondering why I cannot simply cast the return result of .get() to
satisfy the compiler expectation that the method return an object.
Instead I have to assign the result of .get() to a variable and then
return that.

package redick;

import java.util.*;
import java.util.Iterator;

public class Environment {
/*
* map from a variable name to an Object - which may be a datum or a
* procedure
*/
public Map<String,Object> bindings = new HashMap<String, Object>();
public Environment parent;

public void put(String var, Object value) {
bindings.put(var, value);
}
public Object get(String var) {

Object retval = bindings.get(var); // cast not enough
/* Cannot comment this section out */
if (retval == null) {
return (Object)null;
} else {
return retval;
}
/* End required section */ return retval;
}

public String toString() { return bindings.toString(); }

}

As Lew mentioned, you just need to add the "return retval;" (see above)
to the code snippet. Then you should be able to comment out the
indicated section.

Just one of the late-night programmer-didn't-see-it things, I guess... ^_^
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top