Compiler complaining about code that can't be reached, builtin types .equals()
Sean Grimes via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 25 11:26:42 PDT 2015
I have a method, containsValue() in a class similar to
java.util.HashMap.
The class is a template class, instantiated with 2 parameters, K
key, V value.
bool containsValue(V value){
// dlang associative array, .values returns dynamic array of
values in the aa
auto values = this.internal_arr.values;
/*
* isBuiltIn() checks against all the builtin types in D
* returns true if value instantiated as int, double,
immutable(char)[] etc...
* returns false iff value is a user defined object
*/
if(!isBuiltIn(value)){
foreach(val; values){
if(val.equals(value))
return true;
}
}
// isBuiltin() false, assume builtin type, use "==" for
comparison
else{
foreach(val; values){
if(val == value)
return true;
}
}
return false;
}
The problem I'm having is using a D builtin type for the value
parameter. The compiler tells me "no property 'equals' for type
'string'" in the containsValue() method. Well that's well and
good, but I already know that which is why I check if the class
has been instantiated with a builtin type or a user defined type.
My question: Is there any way around the compiler complaining
about this? The code doesn't allow (as best I can tell) the
.equals() method to be called when "value" is a builtin type, so
why does the compiler still complain?
As a side note, I couldn't get std.traits.isBuiltinType(T) to
work, so the function isBuiltIn() is not just calling
std.traits.isBuiltinType(T), it's checking the typeid(value)
against D builtin typeids.
More information about the Digitalmars-d-learn
mailing list