Type checking (instanceof)
Demonstrates runtime type checking; Go uses type assertions, Rust replaces it with enums plus pattern matching.
C C23
#include <stdio.h>
// C has no runtime type information; hand-written "type tags" emulate instanceof
typedef enum {
TYPE_ANIMAL,
TYPE_DOG
} TypeTag;
typedef struct {
TypeTag type;
} Animal;
typedef struct {
Animal base; // inheritance: the base part is at the head of the sub-struct
} Dog;
int main(void) {
Dog dog = { { TYPE_DOG } };
printf("bool(%s)\n", dog.base.type == TYPE_DOG ? "true" : "false");
// walk up the "inheritance chain": a subtype tag also satisfies the supertype check
printf("bool(%s)\n", dog.base.type == TYPE_ANIMAL || dog.base.type == TYPE_DOG ? "true" : "false");
printf("bool(false)"); // C has no Object root type; the third case is shown for comparison
return 0;
}
bool(true) bool(true) bool(false)
C++ C++26
#include <iostream>
class Animal { public: virtual ~Animal() = default; };
class Dog : public Animal {};
class Cat : public Animal {};
int main() {
Dog dog;
Animal& a = dog; // upcast
std::cout << std::boolalpha;
std::cout << (dynamic_cast<Dog*>(&a) != nullptr) << "\n"; // downcast check: is a Dog
std::cout << (dynamic_cast<Animal*>(&dog) != nullptr) << "\n"; // upcast always holds
std::cout << (dynamic_cast<Cat*>(&a) != nullptr) << "\n"; // unrelated subclass: false
return 0;
}
true true false
C# v11.0.100
var dog = new Dog();
Console.WriteLine(dog is Dog);
Console.WriteLine(dog is Animal); // walk up the inheritance chain
Console.WriteLine(dog is string); // unrelated type
class Animal {}
class Dog : Animal {}
True True False
Go v1.27.4
package main
import "fmt"
type Animal struct{ Name string }
type Dog struct{ Animal }
func describe(x any) string {
if d, ok := x.(Dog); ok { // type assertion: check and extract the concrete type
return "is a Dog: " + d.Name
}
return "not a Dog"
}
func main() {
var x any = Dog{Animal: Animal{Name: "Rex"}}
fmt.Println(describe(x))
fmt.Println(describe("text"))
switch v := x.(type) { // type switch: check multiple types at once
case Dog:
fmt.Println("switch hit Dog:", v.Name)
case string:
fmt.Println("switch hit string:", v)
default:
fmt.Println("unknown type")
}
}
is a Dog: Rex not a Dog switch hit Dog: Rex
Java v27.0.1
public class Main {
static class Animal {}
static class Dog extends Animal {}
public static void main(String[] args) {
Animal a = new Dog();
System.out.println(a instanceof Dog);
System.out.println(a instanceof Animal); // walk up the inheritance chain
// comparing with an unrelated class (e.g. String) is a compile error;
// comparing with an interface is legal: Animal does not implement Runnable, so the result is false
System.out.println(a instanceof Runnable);
// Java 16+ supports pattern variables: if (a instanceof Dog d) { d.bark(); }
}
}
true true false
JavaScript v26.13.1
class Animal {}
class Dog extends Animal {}
const dog = new Dog();
console.log(dog instanceof Dog);
console.log(dog instanceof Animal); // walk up the prototype chain
console.log(typeof dog); // use typeof for primitive type checks
console.log(typeof "text");
true true object string
PHP v8.6.0
class Animal {}
class Dog extends Animal {}
$dog = new Dog();
var_dump($dog instanceof Dog);
var_dump($dog instanceof Animal); // walk up the inheritance chain
var_dump($dog instanceof stdClass);
bool(true) bool(true) bool(false)
Python v3.15.2
class Animal:
pass
class Dog(Animal):
pass
dog = Dog()
print(isinstance(dog, Dog))
print(isinstance(dog, Animal)) # considers the inheritance chain (more common in duck-typed code)
print(isinstance(dog, str))
# for the exact type (excluding subclasses) use type(dog) is Dog
True True False
Rust v1.99.1
// Rust has no inheritance and rarely does runtime type checks;
// polymorphic data is carried by enums, dispatched via match (exhaustiveness checked at compile time)
enum Pet {
Dog(String),
Cat(String),
}
fn main() {
let pets = [Pet::Dog(String::from("Rex")), Pet::Cat(String::from("Kitty"))];
for p in &pets {
match p {
Pet::Dog(name) => println!("dog: {}", name),
Pet::Cat(name) => println!("cat: {}", name),
}
}
// for downcasting trait objects, Any + downcast exists but is not idiomatic
}
dog: Rex cat: Kitty
TypeScript v26.13.1
class Animal {}
class Dog extends Animal {}
const dog = new Dog();
console.log(dog instanceof Dog);
console.log(dog instanceof Animal); // walk up the prototype chain
console.log(typeof dog); // use typeof for primitive type checks
console.log(typeof "text");
true true object string
Language differences
C: no runtime type information (RTTI) — the example uses a hand-written type tag checked along the "inheritance chain" to mimic instanceof.
C++: checks inheritance with dynamic_cast plus a null check (the base must be polymorphic).
Go: no inheritance — type assertions x.(T) and type switch are used on interface values/any.
Java: instanceof (Java 16+ supports pattern variables like if (a instanceof Dog d)).
JavaScript: instanceof walks the prototype chain, and typeof is used for primitives.
PHP: instanceof (checks along the inheritance chain).
Python: isinstance(obj, cls) also considers subclasses, while type(x) is T checks the exact type; the more Pythonic approach is duck typing.
Rust: no idiomatic runtime type checking — express it with an enum plus match pattern matching, where the compiler guarantees exhaustiveness; downcasting trait objects is possible via Any, but is generally discouraged.