Generics in return types

K

kelvSYC

One application of generics is probably in a return type, for example

public class Delegate<Ret> {
public Ret dothis() {
// ...
}
}

This of course implies that something is to be returned. Now, this
has probably been asked before, but what is a safe way to simulate a
void delegate? Would it be to do something like:

public class VoidDelegate extends Delegate<Void> {
public Void dothis() {
// ...
return null; // Void is non-instanced, so that's the only
thing we can return
}
}
 
R

RedGrittyBrick

kelvSYC said:
One application of generics is probably in a return type, for example

public class Delegate<Ret> {
public Ret dothis() {
// ...
}
}

This of course implies that something is to be returned. Now, this
has probably been asked before, but what is a safe way to simulate a
void delegate? Would it be to do something like:

public class VoidDelegate extends Delegate<Void> {
public Void dothis() {
// ...
return null; // Void is non-instanced, so that's the only
thing we can return
}
}

I came across this recently with SwingWorker. From memory, Eclipse
objected to every Void/void combo I tried except for

new SwingWorker<Void, Void>() {
protected Void doInBackground() {
...
return null;
}
protected void done() {
...
}
}

Note the use of small-v void and no return for done().
This seems odd to me but maybe someone can make sense of it?
 
K

kelvSYC

Note the use of small-v void and no return for done().
This seems odd to me but maybe someone can make sense of it?

That's because done() javax.swing.SwingWorker is not generic.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top