www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - d strings are the bane of my existance

reply Chris Katko <ckatko gmail.com> writes:
All I want:

```d
string ip_address = "192.168.1.1";
auto x = new InternetAddress( ip_string, "8008");
```

```d
source/app.d(161,16): Error: none of the overloads of `this` are 
callable using argument types `(string, int)`
/usr/include/dmd/phobos/std/socket.d(1472,5):        Candidates 
are: `std.socket.InternetAddress.this()`
/usr/include/dmd/phobos/std/socket.d(1519,5):                     
    `std.socket.InternetAddress.this(scope const(char)[] addr, 
ushort port)`
/usr/include/dmd/phobos/std/socket.d(1542,5):                     
    `std.socket.InternetAddress.this(uint addr, ushort port)`
/usr/include/dmd/phobos/std/socket.d(1550,5):                     
    `std.socket.InternetAddress.this(ushort port)`
/usr/include/dmd/phobos/std/socket.d(1562,5):                     
    `std.socket.InternetAddress.this(sockaddr_in addr)`
```

So InternetAddress cannot be constructed with a string. Only a 
const(char)[]. But toStringz gives me a immutable(char)*, which 
sounds like the same thing but isn't. and .dup on that just 
explodes. cast doesn't seem to work.  to! doesn't seem to work.

I know there "is" a solution, it's just so odd to have this much 
difficulty using a string.
Dec 05 2021
next sibling parent reply rikki cattermole <rikki cattermole.co.nz> writes:
The string is not the problem.

```d
string ip_address = "192.168.1.1";
auto x = new InternetAddress(ip_address, 8008);
```

That works.

A string in D is an alias for immutable(char)[]. This is defined in 
druntime (object.d).

Immutable does cast to const implicitly, so a string argument to the 
constructor works fine as it has the same meaning.

The port however, that needs to be a ubyte/ushort to pass in and not be 
a string like you had it.
Dec 05 2021
parent reply Chris Katko <ckatko gmail.com> writes:
On Sunday, 5 December 2021 at 16:32:16 UTC, rikki cattermole 
wrote:
 The string is not the problem.

 ```d
 string ip_address = "192.168.1.1";
 auto x = new InternetAddress(ip_address, 8008);
 ```

 That works.

 A string in D is an alias for immutable(char)[]. This is 
 defined in druntime (object.d).

 Immutable does cast to const implicitly, so a string argument 
 to the constructor works fine as it has the same meaning.

 The port however, that needs to be a ubyte/ushort to pass in 
 and not be a string like you had it.
Yes! Thank you! I just realized the latter part was broken when I switched to using a uint for the addr. But I didn't know string is an alias for immutable(char)[]! Thank you!
Dec 05 2021
parent Ivan Kazmenko <gassa mail.ru> writes:
On Sunday, 5 December 2021 at 16:37:21 UTC, Chris Katko wrote:
 Yes! Thank you! I just realized the latter part was broken when 
 I switched to using a uint for the addr. But I didn't know 
 string is an alias for immutable(char)[]! Thank you!
Yeah, a `const(char)[]` argument is designed to accept both `immutable(char)[]` (strings) and just `char[]` (mutable arrays of chars) for the arguments. Ivan Kazmenko.
Dec 06 2021
prev sibling next sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 12/5/21 11:24 AM, Chris Katko wrote:
 All I want:
 
 ```d
 string ip_address = "192.168.1.1";
 auto x = new InternetAddress( ip_string, "8008");
 ```
 
 ```d
 source/app.d(161,16): Error: none of the overloads of `this` are 
 callable using argument types `(string, int)`
 `std.socket.InternetAddress.this(scope const(char)[] addr, ushort port)`
 ```
I know you have solved your problem, but take a look at the error message here -- "not callable using argument types `(string, int)`", whereas your sample has types `(string, string)`. It always helps to post *exactly* the code that is causing the problem, not code that you thought you used. If you want to make it simpler for posting, test the simpler code. I'm assuming to get that error you did something like: ```d auto port = 8008; auto x = new InternetAddress( ip_string, port); ``` This doesn't work, because port is inferred to be `int`, not `ushort`. You can fix by using `ushort` instead of `auto`, or using the literal right in the call. -Steve
Dec 06 2021
prev sibling parent forkit <forkit gmail.com> writes:
On Sunday, 5 December 2021 at 16:24:34 UTC, Chris Katko wrote:
 I know there "is" a solution, it's just so odd to have this 
 much difficulty using a string.
Paying attention to the online docs would help you too ;-) https://dlang.org/library/std/socket/internet_address.this.html But, in the event you do 'need' to work with strings, you can just convert the string to ushort. // --- module test; import std.stdio : writeln; import std.socket : InternetAddress; import std.conv : to; void main() { string host = "localhost"; string port = "8080"; auto ia = new InternetAddress(host, to!ushort(port)); writeln(ia); } //-----------
Dec 06 2021