digitalmars.D.learn - "string" data type with readln
- pascal111 (6/6) Jul 25 2022 I tried to type small program, and tried to use "string" data
- Adam D Ruppe (4/7) Jul 25 2022 the overload you used modifies the array you give it
- rikki cattermole (16/16) Jul 25 2022 The version of readln you are using is[0]. This works by taking in a
I tried to type small program, and tried to use "string" data type with "readln", but the compiler refused it, and I had to use "char[]". So, if we can't use "string" with "readln", so what's its benefit? why we need to use it? Code source: https://github.com/pascal111-fra/D/blob/main/proj01.d
Jul 25 2022
On Monday, 25 July 2022 at 19:55:40 UTC, pascal111 wrote:I tried to type small program, and tried to use "string" data type with "readln", but the compiler refused it, and I had to use "char[]".the overload you used modifies the array you give it try string s = readln();
Jul 25 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
Jul 25 2022