Giter VIP home page Giter VIP logo

Comments (6)

datanoise avatar datanoise commented on August 19, 2024

i think you meant to write it as @client not just client

from mongo.cr.

elrok123 avatar elrok123 commented on August 19, 2024

@datanoise Nope, unfortunately not, I offload the @client instance to a local var, due to the read write differences between instance vars and local vars, I've had issues in the past with Crystal not allowing me to set instance vars etc, so tried offloading to a local var just to see if that fixed it.

If you read the code though, you can see here that I definitely define the client var before being used:

-»»»def get_collection(collection : String)                                                         
-»»»-»»»if [email protected]? && !collection.nil?                                                        
-»»»-»»»-»»»client = @client                                                                        
-»»»-»»»-»»»return client.not_nil![collection] if collection.not_nil!                               
-»»»-»»»else                                                                                        
-»»»-»»»-»»»return Nil                                                                              
-»»»-»»»end                                                                                         
-»»»end

from mongo.cr.

datanoise avatar datanoise commented on August 19, 2024

why Nil is uppercased? is it intentional?

from mongo.cr.

elrok123 avatar elrok123 commented on August 19, 2024

In this instance they would resolve to the same thing anyway, no? As it's a return value, and Nil is just the class, and nil is an global instance of the Nil class. Correct me if I'm wrong.

from mongo.cr.

datanoise avatar datanoise commented on August 19, 2024

No, they are not the same thing. 'Nil' is the class type (in this case struct type) and nil is its singleton instance. Nil.not_nil! == Nil as for any other object, but nil.not_nil! will raise an exception. That means that your @client variable could be either an instance of Mongo::Client class or Nil class type and the compiler correctly detects that Nil class type has no method [] defined.

from mongo.cr.

elrok123 avatar elrok123 commented on August 19, 2024

Ah awesome, thanks for the explanation, was curious what was actually going on under the hood, as I've experienced something similar to this I think, and I did not even consider the difference between Nil and nil

from mongo.cr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.