golang nil pointer assignment with ignored output - (*)(nil) [duplicate] - pointers

This question already has answers here:
What does an underscore and interface name after keyword var mean?
(2 answers)
Closed 4 years ago.
while going through this tutorial on writing FUSE filesystems in go, I came across this cryptic assignment:
var _ fs.Node = (*Dir)(nil)
Can someone explain the mechanics of this syntax, and how does it fit in the context in which it is declared? As I understand the outcome of the assignment is actually ignored (and what does the right hand expression even result in? a nil Dir pointer?)

This makes the compiler check if type *Dir fulfills fs.Node interface.
Take a nil pointer, make it a *Dir pointer and assign it to an unnamed variable of interface type fs.Node. Since we never use this variable, we have to make it unnamed.

Related

We can change immutable variable in Elixir. Can we not say rebind equals mutable? [duplicate]

This question already has answers here:
Are Elixir variables really immutable?
(5 answers)
Closed 1 year ago.
I am new for function language. i practised some thing but i wonder one thing. Elixir says me that everything is immutable but i can change variable in elixir. Is there something that I dont know about it?
My example is
defmodule Practise do
def boxes(total) do
size_50 = div(total, 50)
total = rem(total, 50)
{"big: #{size_50} total: #{total}"}
end
end
I can change total variable with new value in same named function. So that i think it is immutable. Is it correct?
Reusing variable names (which is often referred to as rebinding) is just a convenience in Elixir - it's equivalent to using a temporary variable but with the same name. The reference to your original total gets lost in the scope of the function Practice.boxes. But that doesn't matter to you since you don't need it again - you just need the new one.
This is a rare concession made by the designers of Elixir towards imperative programming. Indeed, an expression such as x = x + 1 might be mistaken for pattern matching phrase.

What is the owner of literal reference such as &4 in Rust? [duplicate]

This question already has answers here:
In Rust, can you own a string literal?
(1 answer)
Why can I return a reference to a local literal but not a variable?
(1 answer)
Why is it legal to borrow a temporary?
(3 answers)
Closed 3 years ago.
Normally, there is only one owner for a specific value (except for things like Rc<T>). Then what is the owner of the value 4 below since the variable myVar borrows it from something? I want to know what is that something.
let myVar = &4;
Literals, be they:
number literals, like 4
string literals, like "Hello, World"
Have a 'static lifetime as their value is hard-coded into the library or executable itself. For example, on Linux, they would be found either in the .text segment or the .rodata segment of the ELF binary.
In that sense, you can think of them as being owned by the program itself.

Qt:Add a list of QGraphicsPixmapItems to a scene group? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it ok to cast a STL container with Base type to Derived type?
This should be a quick question... If I have a container, say an STL list, of a base class, is it possible to casts the entire container to a type of subclass? E.g.
[A inherits from base class B]
list<B*> list1;
list1.push_back(new A());
list<A*> list2 = list1;
The compiler is complaining: "conversion from std::vector<B*, std:allocator<B*> >' to non-scalar type 'std::vector<Bar*, ...>' requested."
Is this possible, or am I'm just screwing it up?
No. std::list<T> and std::list<U> are completely different, incompatible types, if std::is_same<T,U>::value is false, i.e if the type arguments T and U aren't same.
Use std::transform and do appropriate casting in the callback.
No, this isn't directly possible because lists with different template types are completely unrelated containers.
If you know that all the items in list1 are actually the child class A then just make list1 contain them as such up front. If you don't know that they're all of the child class then the conversion you want to do is illegal.
What are you really trying to do here?
The safer way to do this is using std::transform.
But, since inside the implementation of std::list, T=A* or T=B* behaves the same way (both have the same size), a list has same inner structure than a list. So it is possible to do the following trick:
list<B*> list2 = *( reinterpret_cast< list<B*>* >(&list1) );
Note:
My answer is informational, please stick with the std::transform approach. Also, I tested this with GCC and it works, but I am not sure if in others STDC++ implementation the list behave the same way.

Using "where" syntax to parametrize a function in Julia [duplicate]

This question already has an answer here:
`where` in function definitions in julia-0.6
(1 answer)
Closed 5 years ago.
Reading the Julia documentation on parametric methods, I can't for the life of me figure out the difference between these two definitions
julia> function f{T<:Real}(x::T)
println("$x with type $T")
end
julia> function g(x::T) where {T<:Real}
println("$x with type $T")
end
Any guidance on the semantic difference between these two definitions would be highly appreciated.
The former is deprecated (in most instances) for the latter. where replaces the old syntax in v0.7 and onwards, and the first will not exist in 1.0.
One exception is inner constructors. The first syntax will still exist for them. But in that case type parameters means something very different. Example: Array{Float64,2}() the inner constructor takes in the parameters from the user. This was confusing before because type parameters had a dual meaning for these different constructs, but now this way of parameterizing only exists for inner constructors and only means this, whereas everything else uses where.

Using macros as functions [duplicate]

This question already has answers here:
How does one reduce a list of boolean values in Common Lisp?
(3 answers)
Closed 8 years ago.
I'm new to Common Lisp, so this got me a bit stumped and Google has failed me.
I have a function sizzle defined like
(defun sizzle (f &rest r) ...blah blah...)
And now I just need to check if all optional arguments are non-nil, so naturally I did
(apply #'and r)
...and then it turns out that and isn't a function, it's a macro (which I haven't got around to just yet).
So, my question is, is there a way to use macros as functions (much like above), or should I just make my own function to check if all values is a given list are non-nil? Or is there yet another approach I haven't thought of?
You can't use macros as functions (that's why it's better to make some things functions, and that's why compiler macros instead of regular macros are used for optimization).
I would use (every #'identity r) or (notany #'null r) instead of writing my own AND function for your example.

Resources