digitalmars.D.learn - writeln the struct from the alis this Example from the home page
- Martin Tschierschke (31/31) Nov 18 2021 Hello, if you take the example from the home page, with the
- Paul Backus (16/47) Nov 18 2021 You can define a `toString` method, like this:
- Jordan Wilson (3/21) Nov 18 2021 I thought private was to the module/file, not class/struct?
- Steven Schveighoffer (3/25) Nov 18 2021 `writeln` is not in your module.
- Martin Tschierschke (2/20) Nov 19 2021 Thank you, just removing ``private `` and it worked!
Hello, if you take the example from the home page, with the additional last line: ```d struct Point { private double[2] p; // Forward all undefined symbols to p alias p this; double dot(Point rhs) { return p[0] * rhs.p[0] + p[1] * rhs.p[1]; } } void main() { import std.stdio : writeln; // Point behaves like a `double[2]` ... Point p1, p2; p1 = [2, 1], p2 = [1, 1]; assert(p1[$ - 1] == 1); // ... but with extended functionality writeln("p1 dot p2 = ", p1.dot(p2)); // additional line: writeln(p1); // is not possible ! } ``` /usr/include/dmd/phobos/std/format.d(3193): Error: no [] operator overload for type Point .. ... How to define, that for Point the same formatting should be used as for double[2] ?
Nov 18 2021
On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote:Hello, if you take the example from the home page, with the additional last line: ```d struct Point { private double[2] p; // Forward all undefined symbols to p alias p this; double dot(Point rhs) { return p[0] * rhs.p[0] + p[1] * rhs.p[1]; } } void main() { import std.stdio : writeln; // Point behaves like a `double[2]` ... Point p1, p2; p1 = [2, 1], p2 = [1, 1]; assert(p1[$ - 1] == 1); // ... but with extended functionality writeln("p1 dot p2 = ", p1.dot(p2)); // additional line: writeln(p1); // is not possible ! } ``` /usr/include/dmd/phobos/std/format.d(3193): Error: no [] operator overload for type Point .. ... How to define, that for Point the same formatting should be used as for double[2] ?You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method.
Nov 18 2021
On Thursday, 18 November 2021 at 16:08:22 UTC, Paul Backus wrote:On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote:I thought private was to the module/file, not class/struct? Jordan[...]You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method.
Nov 18 2021
On 11/18/21 2:58 PM, Jordan Wilson wrote:On Thursday, 18 November 2021 at 16:08:22 UTC, Paul Backus wrote:`writeln` is not in your module. -SteveOn Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote:I thought private was to the module/file, not class/struct?[...]You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method.
Nov 18 2021
On Thursday, 18 November 2021 at 16:08:22 UTC, Paul Backus wrote:On Thursday, 18 November 2021 at 13:51:42 UTC, Martin Tschierschke wrote:Thank you, just removing ``private `` and it worked![...]You can define a `toString` method, like this: ```d string toString() { import std.conv; return p.to!string; } ``` You can find more information about `toString` in the documentation here: https://dlang.org/phobos/std_format_write.html By the way, the reason your original version does not work is that `p` is `private`, so `writeln` cannot access it. If you change `p` to be `public`, it will work without a `toString` method.
Nov 19 2021