How to get the type of anything in Crystal? - crystal-lang

In Javascript the use of typeof() allows you to quickly see what type of anything is, what is the equivalent in the Crystal?
Say the code below I wanted to check what type Crystal thought the object was.
get "/" do
object = {hello: "hello", world: "world"}
object.to_json
puts typeof(object)
end

Sometimes google works wonders, I searched the API docs for typeof and found nothing.
It is just typeof() https://crystal-lang.org/reference/1.4/syntax_and_semantics/typeof.html

Related

Get OID (types::b_oid) as string in MongoDB C++ driver

I'm using MongoDB C++ driver (version 3.4.0 for reference). I need to get the _id of a given document. First, I grab the document:
bsoncxx::stdx::optional<bsoncxx::document::value> sub = conn["mydb"]["csubs"].find_one(...);
so I can access to the _id this way:
sub->view()["_id"].get_oid();
So far, so good.
As far as I have read in driver API this object is of type types::b_oid. However, I would need to get it as std::string.
Surprisingly, I haven't found any method in the types::b_oid class documentation for string conversion. I mean, the typical to_string() method so I can call something like:
sub->view()["_id"].get_oid().to_string();
Probably I'm missing something (because the use case seems to be too obvious :), but after a while checking documentation I haven't find the solution. Any help is welcome!
I think you can call to_string() from the value field:
sub->view()["_id"].get_oid().value.to_string();
Here's an example from the mongocxx github repo

Where does "this.get('filter')" comes from?

I've been following tutorials from Embers website and at certain point they do:
let filterInputValue = this.get('value');
let filterAction = this.get('filter');
filterAction(filterInputValue).then((filterResults) => this.set('results', filterResults));
And as far as I know this.get('filter') is the same thing as doing this.filter or this['filter'], right? I've been looking to their documentation about Component and its base object, since attributes/functions can be inherited, to find out about this filter but was unable to find it.
So, where does filter comes from?
It refers to the action specified in the template. It's there in rentals.hbs in the very first code block of the page you linked:
{{#list-filter
filter=(action 'filterByCity')
edit: to answer your question about this.prop vs get(this, prop) or this.get(prop), this.prop is not the same. however, soon it may well be. check out this RFC for more info

when do you use getGraphObject and getGraphEdge

I'm working on a php based site and integrating some facebook sdk into it to get some basic user information. One thing I've been running into is, when I make a request to the graph, how do I know if I should be using getGraphObject or getGraphEdge? I'm not seeing anything intuitive yet to tell me that.
I'm running the php-sdk4 -> version 5
Here's one example that I ran into last night..
$response = $fb->get('/me/friends', $fbToken);
$fbfriends = $response->getGraphEdge()->asArray();
In the documentation, if you look at the php example, they use getGraphObject. But when I use it, I get an error telling me I should probably use the Edge. There are a lot of "get" functions, but I don't see anything that tells me how to know what to use. getGraphObject, getGraphEdge, getGraphUser, etc. I'd love some insight into this one, because it's been a guessing game for me.
Basicly, when:
Getting /{node-id}, you should use getGraphNode() (getGraphObject() is deprecated)
Getting /{node-id-with-known-type}, you should use getGraph{Type}().
For example, getting /me then use getGraphUser(), getting /{event-id} then use getGraphEvent()
Getting /{node-id}/{edge-name}, you should use getGraphEdge() (getGraphList() is deprecated)
Getting /{node-id}/{edge-name-with-children-known-type}, you should use getGraphEdge({children-type-class}).
For example, getting /me/albums then use getGraphEdge('GraphAlbum')
Nodes class name are in Facebook\GraphNodes namespace.

How to create generic type reference with Roslyn?

I want to create a reference to "System.IEquatable<MyType>", where the open generic type can be reached via typeof and the generic argument as text only. How do I create a correct NameSyntax that can be used as field type, etc.?
We recently used SF.ParseName which creates a QualifiedNameSyntax (not a GenericNameSyntax what wonders me). However, I think this is not ideal, since I'm dealing with <, > and string.Join on my own.
If you are doing this in the workspace layer, you can also use the SyntaxGenerator type to do this in a language independent way:
var generator = SyntaxGenerator.GetGenerator(document);
generator.QualifiedName(generator.IdentifierName("System"),
generator.GenericName(generator.IdentifierName("IEquatable"),
new [] { generator.IdentifierName("MyType") }));
This will generate System.IEquatable< MyType> for C# and System.IEquatable(Of MyType) for VB documents.
You can use the following pattern:
SF.GenericName(
SF.Identifier(#"IEquatable"))
.WithTypeArgumentList(
SF.TypeArgumentList(/.../))

Using Type-safe URLs with setMessage? (shamlet versus hamlet)

How do I use a type-safe url with setMessage?
I want to change
...
setMessage [shamlet|<span .warning>Warning! See Help.|]
...
to a message that contains a link.
From what I could gather thus far, it ought to work somehow like this
...
renderer <- getUrlRender
let html = [hamlet|<span .warning>Warning! See #
<a href=#{HelpR}> Help!|]
setMessage $ toHtml $ html renderer
...
but that code just gives me confusing error messages all over the file.
I did read the printed Yesod Book Chapter on Shakespearian Templates, but I found that it is not very explicit on the involved types. For instance what type does [hamlet|...|]| produce? Without URL-Interpolation, ghci reports t -> Markup but with URL-Interpolation inside, I just get errors.
I am further confused by all the type synonyms involved, e.g. [shamlet|...|] delivers something of type Html, while setMessage expects a Html (). I do not know how to look these up easily: Hoogle often finds nothing on the topic, while Google always finds possibly outdated versions (with examples that no longer work) - sure I get to the newest version eventually, but is there a place where I get an easy overview over these? (Can ghci list all synonyms for a type?)
Note that I actually want to produce the message in a purely functional code fragment, which is later on used by a handler. So that is why I would like to separate the URL rendering from where the hamlet is specified. Thanks for any pointer in the right direction!
I think you want to use getUrlRenderParams. Strangely enough, a related discussion came up on IRC today. Hamlet templates take a URL rendering function as their first argument, and that function must take two parameters: a type-safe URL, and a list of query string parameters. getUrlRender returns a function that doesn't take the query string parameters, which is why you need getUrlRenderParams instead.