Is an automatic string variable a dynamic array in the sense of sec. 10.11.2 "Identity Expression" par. 4?
Salih Dincer
salihdb at hotmail.com
Fri Jul 15 15:14:20 UTC 2022
On Friday, 15 July 2022 at 13:34:03 UTC, kdevel wrote:
>
> In this piece of code [2]:
>
> ```
> void main ()
> {
> string s = null;
> string t = "";
> assert (s is t);
> }
> ```
>
> the assertion trips although both conditions of the given rule
> are met.
```d
string s, t;
void main()
{
assert(s is t);
assert(s is null);
assert(t is null);
s.length = 2;
t.length = 2;
assert(!is(s == t)); // true (check identy)
assert(s == t); // true (check content)
// because they are different strings:
assert(is(string : typeof(s)));
assert(is(string : typeof(t)));
// but it can be same:
auto u = s[0..2]; // a dynamic array
assert(s is u); // true
assert(s == u); // right on
import std.stdio; // see following:
writeln(cast(ubyte[])s); // [255, 255]
writeln(cast(ubyte[])t); // [255, 255]
writeln(cast(ubyte[])u); // [255, 255]
```
SDB%79
More information about the Digitalmars-d
mailing list