Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #94 Print the type of a variable

Print the name of the type of x. Explain if it is a static type or dynamic type.

This may not make sense in all languages.

(type x)
#include <typeinfo>
std::cout<<typeid(x).name();
System.Console.WriteLine( x.GetType() );
import std.stdio: writeln;
writeln(typeid(x));
print(x.runtimeType);
[{_, type} | _] = IEx.Info.info(x)
type
fmt.Printf("%T", x)
import "reflect"
fmt.Println(reflect.TypeOf(x))
import Data.Dynamic
print (dynTypeRep (toDyn x))
console.log (x == null ? x + '' : x.constructor.name);
console.log(typeof x);
System.out.println(((Object)x).getClass().getName());
import static java.lang.System.out;
var s = "abc";
out.println(s.getClass().getSimpleName());
println(x::class.simpleName)
(describe x)
print(type(x))
echo is_object($x) ? get_class($x) : gettype($x);
{$mode objfpc}{$H+}
{$ModeSwitch ImplicitFunctionSpecialization }
 
generic procedure PrintTypeOfX<T>(const X: T);
var
  tk: TTypeKind;
begin
  tk := System.GetTypeKind(X);
  writeln(tk);
end;
print ref($x)||"SCALAR", "\n";
print(x.__class__)
print(type(x))
puts x.class
#![feature(core_intrinsics)]
fn type_of<T>(_: &T) -> &'static str {
    std::intrinsics::type_name::<T>()
}

println!("{}", type_of(&x));

New implementation...
< >
programming-idioms.org