"string" data type with readln

rikki cattermole rikki at cattermole.co.nz
Mon Jul 25 20:21:18 UTC 2022


The version of readln you are using is[0]. This works by taking in a 
buffer of memory to write out, and returns how many codepoints were stored.

Because you are not reusing memory, not using this form you can of 
course use string[1] instead, rather than ``char[]``.

```d
string s = readln();
```

The definition of string is[2]:

```d
alias string  = immutable(char)[];
```

Note the immutable there, which means that each value in the slice 
cannot be modified. Hence why it can't be used as a buffer.

[0] https://dlang.org/phobos/std_stdio.html#.readln.2
[1] https://dlang.org/phobos/std_stdio.html#.readln
[2] https://github.com/dlang/dmd/blob/master/druntime/src/object.d#L69


More information about the Digitalmars-d-learn mailing list