Take the following line of code:
Set<Object> accounts = new Set<Account>();
When I try to execute this in developer console I get an error Illegal assignment from SET to SET
Same error applies if I use a non-SObject type:
Set<Object> strings = new Set<String>(); // Illegal assignment from SET to SET
What has me confused is that we are able to do this with Lists, so the following is valid code:
List<Object> accounts = new List<Account>();
List<Object> strings = new List<String>();
I ran into this problem while trying to write a helper class as follows with an isEmpty() method I could call and pass it any type of collection.
class Util {
public static Boolean isEmpty(Set<Object> collection) {
return collection.isEmpty();
}
public static Boolean isEmpty(List<Object> collection) {
return collection.isEmpty();
}
public static Boolean isEmpty(Map<Object, Object> collection) {
return collection.isEmpty();
}
}
By the way, a similar problem occurs for Maps:
Map<Object, Object> accounts = new Map<Id, Account>(); // Illegal assignment from MAP to MAP
Map<Object, Object> strings = new Map<String, String>(); // Illegal assignment from MAP to MAP
Anyone know why this is the way it is? Are there some technical reasons? Is there any way around this?