To match your literal request you need Alternatives
rather than Or
.
Either x : (_Integer | _Real)
or x_Integer | x_Real
will work.
Following what Szabolcs and "Guess who it is" wrote you might define a realQ
like so:
realQ = NumericQ[#] && Im[#] == 0 &;
f[x_?realQ] := x^2
f /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}
{1, π^2, 1.69, 4/9, f[x^2], f[7.1 - 2.8 I]}
Of note for those who are comfortable using undocumented functions:
Internal`RealValuedNumericQ /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}
{True, True, True, True, False, False}
There is also Internal`RealValuedNumberQ
which passes only explicit numbers:
Internal`RealValuedNumberQ /@ {1, Pi, 1.3, 2/3, x^2, 7.1 - 2.8 I}
{True, False, True, True, False, False}
x : (_Integer | _Real )
. Look upAlternatives[]
. $\endgroup$Integer
andReal
are data types. Are you sure this is what you want to check for? This is not the same as determining whether an arbitrary expression is integer or real (or rational or complex). Neither ofPi
,Sqrt[2]
,2/3
are either ofInteger
orReal
type, but they are all real numbers. $\endgroup$Alternatives
rather thanOr
. Eitherx : (_Integer | _Real)
orx_Integer | x_Real
will work. $\endgroup$NumberQ[]
/NumericQ[]
along with a test likex_ /; Im[x] == 0
. $\endgroup$