« first day (4112 days earlier)      last day (828 days later) » 

12:00 AM
Faith may be defined briefly as an illogical belief in the occurence of the improbable. - H. L. Mencken (source)
 
 
2 hours later…
2:14 AM
uses java.io.File
 
 
5 hours later…
6:50 AM
posted on January 25, 2022

 
 
5 hours later…
11:28 AM
@motaa the "why" is not a restriction by type erasure
it is a restriction of type safety
the solution to which, is higher order types
which, no modern language has
except...
KODIAN!
although, this level of higher order types might be above Kodian's as well
basically, the interface of Map<K, V> is insufficient to supply you with type relation information between K and V
K, which could be a Class<?>, can be assumed to be Class<X> for the values X (where X extends V)
in which case, suppressing the warning because the developer knows more than the compiler, is the correct approach
as a language developer, you might look at the problem and produce a solution for it, but this is a very rare use case
 
@Wietlol ah ok, so the other answers would be the correct approach instead of mine?
 
yes
 
I'll ask for a deletion then, no need to confuse future readers
 
to make it typesafe, the get method can do the cast
 
well I did propose a typesafe approach without suppressing warnings
 
11:42 AM
so, in case of single values, you can do this
public <T> T get(Class<T> type) {
	@SuppressWarnings("unchecked") //It is fine to ignore warnings here
	T res = type.cast(container.get(type));
	return res;
}
instead of
public <T> T get(Class<T> type) {
	@SuppressWarnings("unchecked") //It is fine to ignore warnings here
	T res = (T) container.get(type);
	return res;
}
(not sure if cast actually returns T though)
your solution is for when your keys are not Class<T>
but instead a different type, which should encapsulate the Class<T> and also have a cast function
so the map implementation would be the same
 
yes they encapsulate the Class<T>
 
the problem is that the question is also about not storing single values, but a set
so, you can't just cast using the class
you would have to open the set, cast each value and collect into a set again
 
that's why I proposed inheritence or composition
 
or just assert that the values are correct
inheritance in this case is not a good idea
 
aight, removed
although I liked the approach in my simulation parameters case ^^
the one thing that struck me in the effective java type safe containers is that you could only add one instance of a class
 
11:54 AM
that is how a Map<K, V> works
you can only have one key
 
yes I know that but if you want to store more objects that return an Integer instance, the classic thc does not work anymore
 
true
> the problem is that the question is also about not storing single values, but a set
 
yes. Anyways it was an old question, I just thought I could add some spice to it for future readers ;)
but you are right, from a dev perspective the other approaches are clearer
 
 
6 hours later…
6:03 PM
picks nose
 

« first day (4112 days earlier)      last day (828 days later) »