Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
template<typename T>
void tst(T)
{
using namespace std;
// Strip away any references and pointer
typedef remove_const<remove_pointer<decay<T>::type>::type>::type D;
if(is_same<D, foo>::value)
{
cout << "same type" << endl;
}
else if(is_base_of<foo, D>::value)
{
cout << "extends type" << endl;
}
else
{
cout << "not related" << endl;
}
}
public static void tst(Object x) {
if (x.GetType() == typeof(foo))
Console.WriteLine("Same type.");
else if (x.GetType().IsAssignableTo(typeof(foo)))
Console.WriteLine("Extends type.");
else
Console.WriteLine("Not related.");
}
subroutine tst (x)
class (*), intent(in) :: x
type (foo) :: y
if (same_type_as (x,y)) then
write (*,'(A)') "Same type."
else if (extends_type_of (x,y)) then
write (*,'(A)') "Extends type."
else
write (*,'(A)') "Not related."
end if
end subroutine tst
An alternative could be SELECT TYPE.
<T> void tst(T x) {
Class<?> c = x.getClass();
if (c.getSuperclass() == Foo.class)
out.println("Extends type.");
else if (c == Foo.class)
out.println("Same type.");
else
out.println("Not related.");
}
sub tst {
my ($x) = @_;
if ( $x->isa('Foo') ) {
say "Same type";
}
elsif ( $x->isa('FooExt') ) {
my $issubclass = grep { $_ eq 'Foo' } @FooExt::isa;
say $issubclass ? "Extends type" : "Same type";
}
else {
say "Not related"
}
}
To determine whether a FooExt object inherits from class Foo, we look for Foo in the @isa list. See demo for class definitions.
def tst(x):
if type(x) == foo:
print("Same type.")
elif isinstance(x, foo):
print("Extends type.")
else:
print("Not related.")
def tst(x)
if x.class == Foo then puts "Same type."
elsif x.is_a?(Foo) then puts "Extends type."
else puts "Not related."
end
end
fn type_of<T>(_: &T) -> &str {
std::any::type_name::<T>()
}
if type_of(&x) == type_of(&foo) {
println!("x & foo -> same type");
} else {
println!("x & foo -> not related");
}
Extends type is missing in Rust