!grr ...generics... this would never happen with casting...

T

timasmith

So I have a method with a parameter:

call (Set<IBaseModel> collection) {
...
}


I have a class

class MyModel implements IBaseModel {
...
}


I want to call the method passing a collection of MyModel

Set<MyModel> myset = getMySet();
call (myset);

but I get it cannot do it (method undefined due to incorrect
arguments..)

hmm

any ideas
 
H

hiwa

(e-mail address removed) ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
So I have a method with a parameter:

call (Set<IBaseModel> collection) {
...
}


I have a class

class MyModel implements IBaseModel {
...
}


I want to call the method passing a collection of MyModel

Set<MyModel> myset = getMySet();
call (myset);

but I get it cannot do it (method undefined due to incorrect
arguments..)

hmm

any ideas
(Set<IBaseModel> collection) {
(Set<T extends IBaseModel> collection) {
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hiwa schreef:
public <T> void call(Set<T extends IBaseModel> collection) {
...
}

No need for a generic method here. Better use wildcards:

public void call(Set<? extends IBaseModel> coll){...}

H.

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEzw51e+7xMGD3itQRAn+BAJ4nodrsTQ3k2Ms339EUY2zy898NqgCfRA7K
VzXvIaQELLHKzbB6whirmKs=
=5Pri
-----END PGP SIGNATURE-----
 
O

Oliver Wong

Hendrik Maryns said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

hiwa schreef:

No need for a generic method here. Better use wildcards:

public void call(Set<? extends IBaseModel> coll){...}

It also depends entirely on what the method does. For example, if the
method tries to add to the set, neither of the above will work.

Basically, the solution is for the OP to study generics more and
understand the design implications of the decisions made.

- Oliver
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top