Proposal: Inspect implementation for URI

35 views
Skip to first unread message

Wojtek Mach

unread,
Jan 1, 2018, 10:58:30 AM1/1/18
to elixir-lang-core
Hello,

Having an Inspect implementation would make a shorter output in iex, exunit diffs etc and the string representation is inambiguous. Under the hood it would use `String.Chars.URI.to_string/1`.
The only reason it might have been omitted that I can think of is how it plays with `URI.default_port/2`:

Before:

iex(3)> URI.default_port("ponzi", 8080)
:ok

iex
(4)> URI.parse("http://example.com:8080")
%URI{authority: "example.com:8080", fragment: nil, host: "example.com",
 path
: nil, port: 8080, query: nil, scheme: "http", userinfo: nil}

iex
(5)> URI.parse("ponzi://example.com:8080")
%URI{authority: "example.com:8080", fragment: nil, host: "example.com",
 path
: nil, port: 8080, query: nil, scheme: "ponzi", userinfo: nil}

iex
(6)> URI.parse("ponzi://example.com")
%URI{authority: "example.com", fragment: nil, host: "example.com", path: nil,
 port
: 8080, query: nil, scheme: "ponzi", userinfo: nil}



After:

iex(2)> URI.default_port("ponzi", 8080)
:ok

iex(3)> URI.parse("http://example.com:8080")
#URI<http://example.com:8080>

iex(4)> URI.parse("ponzi://example.com:8080")
#URI<ponzi://example.com>

iex(5)> URI.parse("ponzi://example.com")
#URI<ponzi://example.com>



so one could argue we lose a bit of information as the default port is implicit. Was that the reason it was omitted? Otherwise, I'm happy to prepare a PR.

Ben Wilson

unread,
Jan 1, 2018, 11:33:47 AM1/1/18
to elixir-lang-core
The downside here is that you can no longer copy and paste the output.

OvermindDL1

unread,
Jan 4, 2018, 12:52:46 PM1/4/18
to elixir-lang-core
It could always print out to something like:

```elixir
URI.parse("ponzi://example.com"
```

Thus making the output format the thing needed to execute to recreate the same structure.

OvermindDL1

unread,
Jan 4, 2018, 12:53:14 PM1/4/18
to elixir-lang-core
`)`*

Wojtek Mach

unread,
Jan 4, 2018, 1:05:04 PM1/4/18
to elixir-lang-core
Or `URI["http://example.com"]` - all you need is to implement `Access` for atom which is actually trivial... The caveat with putting "function calls" as inspect representation is that it's not quite copy-pastable as literals because you can't use it in match clauses. But let's leave that discussion for a separate thread!
Reply all
Reply to author
Forward
0 new messages