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

Type inference for conditions? #865

Closed
pyrossh opened this issue Jun 26, 2015 · 4 comments
Closed

Type inference for conditions? #865

pyrossh opened this issue Jun 26, 2015 · 4 comments

Comments

@pyrossh
Copy link

pyrossh commented Jun 26, 2015

I Have a constructor like this,

class Playlyfe
   def initialize(@client_id, @client_secret, @store = nil)
       unless @store
          @store = ->(token : Hash(String, JSON::Type)) { puts "Storing Token" }
       end
   end

  def get_access_token
      token = {} of String => String
      @store.call token
  end
end
in ./playlyfe.cr:83: instantiating 'get_access_token()'

      token = get_access_token()
              ^~~~~~~~~~~~~~~~

in ./playlyfe.cr:67: undefined method 'call' for Nil

    @store.call token
           ^~~~


But I get this compile error shouldn't the compiler know that @store will never be nil.

Anyway I tried this

def initialize(@client_id, @client_secret, @store = ->(token : Hash(String, JSON::Type)) { puts "Storing Token" })
end

But then this gives this this error

in ./playlyfe.cr:67: undefined method 'call' for String

    @store.call token
           ^~~~

And is there an easier way to alias this {} of String => String
I tried alias ReqQuery = Hash of String => String But that doesn't seem to work.

@jhass
Copy link
Member

jhass commented Jun 26, 2015

The alias should just be alias ReqQuery = Hash(String, String). For the other question see #477.

@pyrossh
Copy link
Author

pyrossh commented Jun 26, 2015

Thanks got that instance condition checking thing working. But now when I create an empty hash parameter with this alias

def api(method, route, query = {} of ReqQuery)
end

it gives me this error

x error in ./playlyfe.cr:93: expecting token '=>', not ','

  def api(method, route, query = {} of ReqQuery, body = Object.new, raw = false)                                          ^

Is this not possible or is there some other syntax because I have a lot of methods with this and don't want to redefine the types.

@jhass
Copy link
Member

jhass commented Jun 26, 2015

Simply do query = ReqQuery.new

@pyrossh
Copy link
Author

pyrossh commented Jun 26, 2015

Thanks that works.. Need to learn more on crystal.

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

No branches or pull requests

2 participants