what's the semantics of 'varX is varY', in particular `strX is strY`?
Paul Backus
snarwin at gmail.com
Wed Aug 5 17:33:36 UTC 2020
On Wednesday, 5 August 2020 at 17:29:09 UTC, mw wrote:
> I've thought it's compare by reference, as in e.g. `assert(obj
> !is null)`
>
> but
>
> ```
> string strX = "2020-07-29";
> string strY = "2020-07-29";
> string strZ = "2020-07-30";
> assert(strX !is strY); // assertion failed
> assert(strX !is strZ); // assertion pass
> ```
>
> so here `is` means compare strings by their contents? where is
> the exact definition of the semantics of 'varX is varY'?
It's documented under the name "Identity Expression":
https://dlang.org/spec/expression.html#IdentityExpression
You're correct that it's comparing by reference. The reason strX
and strY are equal is that the compiler noticed you wrote two
identical string literals, so it combined them in the binary. You
can tell this is true because `assert(strX.ptr == strY.ptr)`
passes.
More information about the Digitalmars-d
mailing list