Logo

Programming-Idioms

History of Idiom 94 > diff from v4 to v5

Edit summary for version 5 by :

Version 4

2015-10-28, 02:07:49

Version 5

2015-10-29, 14:05:17

Idiom #94 Print type of 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.

Idiom #94 Print type of 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.

Code
print ref($x)||"SCALAR", "\n";

Example:
my $s = "foo";
my @a = (1,2);
my %h = {foo=>1, bar=>2};
my $o = bless {}, 'OBJECT';
print ref($_)||"SCALAR", "\n" for $s, \@a, \%h, $o;

Output:
SCALAR
ARRAY
HASH
OBJECT
Code
print ref($x)||"SCALAR", "\n";

Example:
my $s = "foo";
my @a = (1,2);
my %h = {foo=>1, bar=>2};
my $o = bless {}, 'OBJECT';
print ref($_)||"SCALAR", "\n" for $s, \@a, \%h, $o;

Output:
SCALAR
ARRAY
HASH
OBJECT
Imports
import "reflect"
Imports
import "reflect"
Code
fmt.Println(reflect.TypeOf(x))
Code
fmt.Println(reflect.TypeOf(x))
Comments bubble
This prints the dynamic type of x.
Comments bubble
This prints the dynamic type of x.
Doc URL
http://blog.golang.org/laws-of-reflection
Doc URL
http://blog.golang.org/laws-of-reflection
Demo URL
http://play.golang.org/p/poA41Rb0Oy
Demo URL
http://play.golang.org/p/poA41Rb0Oy