Thread overview
Type to represent URIs?
Jan 28, 2019
Victor Porton
Jan 28, 2019
bauss
Jan 28, 2019
Victor Porton
Jan 29, 2019
Erikvv
Jan 29, 2019
Atila Neves
Jan 30, 2019
Jesse Phillips
Jan 30, 2019
Neia Neutuladh
Jan 30, 2019
H. S. Teoh
January 28, 2019
Which type should I use to represent (absolute) URIs (or URLs)?

Is it OK to use `string` for this, or is there a better option?

I could also use URI class from my library:
https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d

but as you see in this library there are two choices for URI class and it's unclear which of the two I would use.
January 28, 2019
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
> Which type should I use to represent (absolute) URIs (or URLs)?
>
> Is it OK to use `string` for this, or is there a better option?
>
> I could also use URI class from my library:
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d
>
> but as you see in this library there are two choices for URI class and it's unclear which of the two I would use.

https://dlang.org/phobos/std_uri.html
January 28, 2019
On Monday, 28 January 2019 at 18:36:11 UTC, bauss wrote:
> On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
>> Which type should I use to represent (absolute) URIs (or URLs)?

...

> https://dlang.org/phobos/std_uri.html

This modules does not contain a type to represent URIs.

OK, I will use

alias URI = string;
January 29, 2019
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
> Which type should I use to represent (absolute) URIs (or URLs)?
>
> Is it OK to use `string` for this, or is there a better option?
>
> I could also use URI class from my library:
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d
>
> but as you see in this library there are two choices for URI class and it's unclear which of the two I would use.

It depends. If you're just taking a string from config or user input and passing it to a library, I would just leave it a string.

However, if you are going to manipulate the url, like adding query parameters, changing the path, or checking the protocol, I would use a URI class for that.

http://code.dlang.org/packages/urld looks good to me.
January 29, 2019
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
> Which type should I use to represent (absolute) URIs (or URLs)?
>
> Is it OK to use `string` for this, or is there a better option?
>
> I could also use URI class from my library:
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d
>
> but as you see in this library there are two choices for URI class and it's unclear which of the two I would use.

It depends.

If you have a function such as `void connect(string uri)` with only one parameter, I'd say a string is sufficient. If, however, it's `void fun(string uri, string config, string somethingElse)`, then use a URI type instead.
January 30, 2019
On Monday, 28 January 2019 at 17:53:17 UTC, Victor Porton wrote:
> Which type should I use to represent (absolute) URIs (or URLs)?
>
> Is it OK to use `string` for this, or is there a better option?
>
> I could also use URI class from my library:
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/raptor/uri.d
> https://github.com/vporton/redland-bindings/blob/dlang/dlang/source/rdf/redland/uri.d
>
> but as you see in this library there are two choices for URI class and it's unclear which of the two I would use.

I like std.path it does not use a path type and works directly off strings. There are benefits to a path type, but I don't know if that is enough.

So I think I would prefer a uri library doing the same thing.
January 30, 2019
On Wed, 30 Jan 2019 15:17:28 +0000, Jesse Phillips wrote:
> I like std.path it does not use a path type and works directly off strings. There are benefits to a path type, but I don't know if that is enough.
> 
> So I think I would prefer a uri library doing the same thing.

I admit I didn't even consider that when writing urld. Now that I'm considering it, it seems like a tiny benefit in memory usage at a cost of having to re-parse large portions of the URL each time you want to manipulate it.

If you are only going to extract one part of a URL, it's better to use string everywhere. If you are going to manipulate a URL in several ways in a narrow section of code, you can use string at rest and in transit, then use the types provided by Vibe or urld only when you need to extract data or modify a URL. Or provide overloads for both, maybe.
January 30, 2019
On Wed, Jan 30, 2019 at 05:21:25PM +0000, Neia Neutuladh via Digitalmars-d wrote:
> On Wed, 30 Jan 2019 15:17:28 +0000, Jesse Phillips wrote:
> > I like std.path it does not use a path type and works directly off strings. There are benefits to a path type, but I don't know if that is enough.
> > 
> > So I think I would prefer a uri library doing the same thing.
> 
> I admit I didn't even consider that when writing urld. Now that I'm considering it, it seems like a tiny benefit in memory usage at a cost of having to re-parse large portions of the URL each time you want to manipulate it.
> 
> If you are only going to extract one part of a URL, it's better to use string everywhere. If you are going to manipulate a URL in several ways in a narrow section of code, you can use string at rest and in transit, then use the types provided by Vibe or urld only when you need to extract data or modify a URL. Or provide overloads for both, maybe.

This is the classic case of different representations for the same thing, like using cartesian coordinates vs. polar / spherical coordinates for vectors.  Generally, it would make sense to use the most convenient representation for the particular piece of code you're working with, and interconvert where necessary.


T

-- 
If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell