Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default Type::STREAM for type. #4146

Closed
wants to merge 1 commit into from

Conversation

txdv
Copy link

@txdv txdv commented Mar 14, 2017

It can't be nil, it has to be an integer value.

require "socket"

puts Socket::Addrinfo.resolve("localhost", 27015).first.ip_address.address

throws

Error in info.cr:3: instantiating 'Socket::Addrinfo:Class#resolve(String, Int32)'

puts Socket::Addrinfo.resolve("localhost", 27015).first.ip_address.address
                      ^~~~~~~

in /opt/crystal/src/socket/addrinfo.cr:28: can't restrict Nil to Type

    def self.resolve(domain, service, family : Family? = nil, type : Type = nil, protocol : Protocol = Protocol::IP, timeout = nil) : Array(Addrinfo)

which is defined here

I changed the type to Type? and made sure it would use a default value further down.

It can't be nil, it has to be an integer value.
@txdv
Copy link
Author

txdv commented Mar 14, 2017

Looking at the spec ... maybe it was intended to not specify any default value at all?

But then the default value of nil doesn't make sense and it should be an argument at the beginning of the argument list.

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Mar 14, 2017

This is by design. The type argument is required. It's set to nil but forced to be Type so it's usable as a named argument (i.e. resolve(domain, "http", type: Socket::Type::STREAM) but still required. You'll may prefer to use Addrinfo.tcp or Addrinfo.udp instead.

I suppose we could remove the default nil assignment, since Crystal now allows any argument to be a named argument.

@txdv
Copy link
Author

txdv commented Mar 14, 2017

Hm, I was just confused, because ruby had just an interface like resolve(string), i guess this represents the underlying protocol better

@txdv txdv closed this Mar 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants