Thread overview
d strings are the bane of my existance
Dec 05, 2021
Chris Katko
Dec 05, 2021
rikki cattermole
Dec 05, 2021
Chris Katko
Dec 06, 2021
Ivan Kazmenko
Dec 07, 2021
forkit
December 05, 2021

All I want:

string ip_address = "192.168.1.1";
auto x = new InternetAddress( ip_string, "8008");
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.

December 06, 2021
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.
December 05, 2021
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!
December 06, 2021
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.
December 06, 2021

On 12/5/21 11:24 AM, Chris Katko wrote:

>

All I want:

string ip_address = "192.168.1.1";
auto x = new InternetAddress( ip_string, "8008");
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:

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

December 07, 2021
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);
}

//-----------