I'm looking for a way to get the type of an object:
TypeOf["x"] -> String
TypeOf[1] -> Integer
Or something along those lines.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityExample:
list = {1, 2, 3};
integer = 1;
real = 0.1;
Head @ list
Head @ integer
Head @ real
Output:
List
Integer
Real
Reference
(Post adapted after comments.)
Mathematica 10 introduced a new type system local to Dataset
, that's used like this:
Needs["TypeSystem`"]
DeduceType[{1, Sqrt[2], "test", {1, Sqrt[2], 3}, {1, 2, 3}}]
In this type system a type is not the same as Head[expr]
. In this framework the head of an atomic value is instead retrieved by TypeAtoms
:
But yes, for what you want to do you still need Head
.
DeduceType
is still undocumented. What are actual use cases for this new type system?
$\endgroup$
May 7, 2016 at 20:58
Dataset
. It is not on the same fundamental level as Mathematica's core "type system", of which Head
is part. It has a status of specific type system used by one or several application modules internally, and has no meaning in a more general context of fundamental Mathematica language / building blocks.
$\endgroup$
May 7, 2016 at 21:16
Dataset
, WReach has invoked it in several of his answers to explain different unintuitive behaviors.
$\endgroup$
Integer
, Real
etc. as type atoms, not as types. So whether it is intentional or not they have introduced a new terminology for these labels. Yes, this is nitpicking. I see this as an opportunity to mention TypeSystem
to readers who may not know about it.
$\endgroup$
Compile
also has a type system, which is different from either the new TypeSystem
or the core Mathematica type system. Both Compile
's type system and the new TypeSystem
are incomplete - there are types they can't describe (but core Mathematica can). I agree that it is good to mention these, but the question about types in Mathematica in general has a clear and unique answer, and that answer is - atoms and normal expressions, where heads of expressions one may interpret as non-atomic types.
$\endgroup$
May 7, 2016 at 21:32
Head
. $\endgroup$