On Tuesday, 1 July 2014 at 05:26:47 UTC, Ali Çehreli wrote:
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
Thanks, This code works and my question is that is this a good
practice to use `ref in` with structs instead of traditional
pointer syntax (which does not play well with UFCS though) ? Is
there any perfomance implications with `ref in`? I tried that it
does not seem to copy the parameter value, which is good for me:
```d
#!/usr/bin/rdmd
import std.stdio;
struct Server
{
string name;
}
string prompt(ref in Server server)
{
__server.name = "new name";
return server.name ~ ">";
}
Server __server;
void main()
{
__server.name = "old_name";
writeln(__server.prompt);
}
```
which prints
```
newname>
```
meaning the `return server.name ~ ">"` code in promt is using a
reference of __server instead of copying the value.