Yes. In section 2.4 of the 2010 specification, it says
Underscore,
_
, is treated as a lowercase letter, and can occur wherever a lowercase letter can.
...so _threadId
is a legal identifier according to the language spec which should be utterly portable.
However,
_
all by itself is a reserved identifier, used as wild card in patterns.
...so you can't use _
alone outside patterns, thus can't use that value.
Compilers that offer warnings for unused identifiers are encouraged to suppress such warnings for identifiers beginning with underscore. This allows programmers to use
_foo
for a parameter that they expect to be unused.
So _threadId
is an ordinary identifier which you could use elsewhere, but you shouldn't be warned if you throw it away.
(Exactly the same text is in the Haskell 98 report.)
Examples:
main = do
_two <- return 2
print _two -- works
compiles and prints 2 according to the spec and
main = do
_two <- return 2
print 3 -- no warnings, but oops, didn't use _two
compiles without warning according to the spec, and
main = do
_ <- return 2
print _ -- syntax error: _ used as identifier
is a syntax error according to the spec.