IsExpression
Kirk McDonald
kirklin.mcdonald at gmail.com
Mon Mar 12 17:14:13 PDT 2007
Luís Marques wrote:
> Hello,
>
> The documentation of IsExpression lists the following valid forms:
>
> is ( Type )
> is ( Type : TypeSpecialization )
> is ( Type == TypeSpecialization )
> is ( Type Identifier )
> is ( Type Identifier : TypeSpecialization )
> is ( Type Identifier == TypeSpecialization )
>
> What about the form "expression is expression"? I cannot find
> documentation for that form. What are its semantics? "exp is null" is an
> obvious one. But what about other forms?
>
> - for classes it seems that "obj1 is obj2" is equivalent to "&obj1 ==
> &obj2". Can I rely on that? I have a singleton called "nil", so it's
> great being able to say "obj is nil".
>
> - for structs "structvar1 is structvar2" is always true.
>
> - for PODs it seems that values must be semantically equivalent. For
> instance, "1L is 1.0+0i" is true.
>
> Could this be documented?
>
The 'is' keyword is used in two completely separate contexts: The first
is the IsExpression, which has the grammar you outlined above. This is
used for compile-time type checking.
The other use of 'is' is in the IdentityExpression.
http://www.digitalmars.com/d/expression.html#IdentityExpression
"For operand types other than class objects, static or dynamic arrays,
identity is defined as being the same as equality."
Thus for structs I would expect it to perform a bitwise comparison of
the two instances if the struct doesn't overload opEquals.
> Also, the IsExpression docs refer to a "Type", which can be an
> identifier (e.g. "int x; writefln(is(x));" is valid, even though the
> form in question is "is(Type)"). Perhaps that could be made more clear?
>
It is indeed valid. As you point out, a "Type" can be an identifier, and
it is a property of the IsExpression that it returns false if the Type
provided is not a semantically valid type. Therefore, you must be able
to provide it semantically /invalid/ types. (Which must still be
/syntactically/ valid.)
--
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org
More information about the Digitalmars-d
mailing list