Comments (6)
i think you meant to write it as @client
not just client
from mongo.cr.
@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.
why Nil
is uppercased? is it intentional?
from mongo.cr.
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.
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.
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)
- How to find document by Object ID? HOT 3
- Spec fails with libmongoc 1.4.2 (Debian stretch)
- Unable to build on Debian stretch HOT 6
- Can't convert query result to array HOT 1
- Insert a document with array of hash HOT 1
- How does one execute a command using the 'client.command()' method? HOT 3
- Would be nice to have a folder for some examples? HOT 2
- Mongo.cr and the event loop HOT 3
- undefined method 'to_bson' for JSON::Any (did you mean 'to_json'?) HOT 2
- upsert method
- get id of inserted document HOT 3
- how to work with BSON arrays? HOT 1
- update to work with Crystal 0.24.1 HOT 7
- How to find all documents from collection HOT 2
- [request] upgrade mongo-c driver version HOT 3
- Add support for Change Streams HOT 1
- Unhandled exception: File not open for writing (IO::Error) from usr/share/crystal/src/crystal/system/unix/file_descriptor.cr:23:11 in 'unbuffered_write' HOT 2
- Error when trying to retrieve value by key from BSON object
- Date Range Query
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo.cr.