struct, ref in, and UFCS

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 30 22:26:46 PDT 2014


On 06/30/2014 10:11 PM, Puming wrote:

 > On Tuesday, 1 July 2014 at 05:09:49 UTC, Puming wrote:
 >> Hi,
 >>
 >> I have a struct and want to extends its methods, like:
 >>
 >> ```d
 >> struct Server
 >> {
 >>   string name;
 >>   string ip;
 >>   int port;
 >>   string user;
 >> }
 >> ```
 >>
 >> extension method here:
 >>
 >> ```d
 >> string prompt(ref in Server server)
 >> {
 >>    return server.user ~ "@" ~ server.ip ~ ":" ~ server.port;
 >> }
 >>
 >> ```
 > should be `server.port.to!int`;

I think it should actually be server.port.to!string;

 >> is this the correct way to use struct and UFCS? it does not seem to
 >> copy there.

I don't understand your question but I wanted to help others by making 
complete code from your messages:

import std.conv;

struct Server
{
     string name;
     string ip;
     int port;
     string user;
}

string prompt(ref in Server server)
{
     return server.user ~ "@" ~ server.ip ~ ":" ~ server.port.to!string;
}

void main()
{
     auto server = Server("bzzt", "192.168.0.1", 80, "nobody");
     string p = server.prompt;
}

Ali



More information about the Digitalmars-d-learn mailing list