How to understand this "with"? - ocaml

(** adds an header option in the header option list*)
let add_headers header key value =
{ header with
headers = Http_headers.add key value header.headers }
How to understand "header with headers ..." ? I guess it "replace" the header.headers with a new headers. Howerver, the passed header should be immutable, shouldnot it ?
Sincerely!

Yes, the passed header is (or can be) immutable. This function doesn't actually modify its argument; it just returns a copy of it, with the copy's headers field "replaced", as you say.

Related

mulit netsting `mutable` in Google proto

I have proto file, saying that,
message RecommendInfo{
repeated RecommendItem vec_item = 1;
}
message Response{
RecommendInfo recomInfo = 1;
}
I want to produce type Response response.
So I use following code,
Response response;
*(recommendResponse.recominfo().mutable_vec_item()) = {items.begin(), items.end()};
LOG_INFO << response.DebugString();
But there gets empty vecitem. I thought there existed Response object on stack. Response object includes RecommendInfo object on the stack. Because I want to change items inside RecommendInfo object. So I use mutable_vec_item() to set items.
I try
*(recom_response.mutable_recominfo()->mutable_vecitem()) = {items.begin(), items.end()};
The code works and print complete RecommendInfo object including vec_item.
I can't explain it clearly. When I try to change recom_response.recominfo(), it seems to be wrong. However,isn't RecommendInfo an object on stack? And I just want to modify vec_item.
Modifying vecitem modifies the RecommendInfo it belongs to.
So in order to modify the content of vecitem, you have to be operating on a modifiable (aka mutable) RecommendInfo. That's why you have to use mutable_recominfo() instead of recominfo().

How to share cl::opt arguments between passes?

I have defined a cl::opt parameter in one of my pass.
cl::opt<std::string> input("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"));
I wonder that how to share this parameter with another pass? I have tried to move it to a header file and let the another pass include it, but it reported a multiple definition error.
You need to make an option's storage external.
In some shared header file declare a variable:
extern std::string input;
And define it and the option itself in only one of your sources:
std::string input;
cl::opt<std::string, true> inputFlag("input", cl::init(""), cl::desc("the input file"), cl::value_desc("the input file"), cl::location(input));
Note added cl::location(input) argument, which instructs cl::opt to store the option value in the input variable. Now you can access inputs value from different TUs, but still have only one definition.
See Internal and External storage section.

Lua: Redirect extern function definitions to a specified table

I have one file "example.lua":
local function manipulate(something)
return string.rep(something, 3) -- repeats the given string
end
function apiFunction(somethingelse)
return manipulate(somethingelse)
end
and another files (main.lua) task is to "load"/"do" it:
loadAPI("example.lua", "externAPI") --< the part i need help with
externAPI.apiFunction("Test") --> should return TestTestTest
the thing that should happen is, that example.lua gets executed just like
dofile("example.lua")
but everything globally "defined" within example.lua (in this case the apiFunction) moves to the new generated global "externAPI" table and the rest (ie. manipulate) is hidden and only available from inside the example.lua file.
I've seen this bahaviour before in the minecraft mod "ComputerCraft" in which there is a function called "os.loadAPI("/somepath/sha-2") and it would define the definitions in the sha-2-chunk in the due to the name specified "sha-2"-table.
I've been searching for this sort of scoping/redirecting stuff for a while but there are no solutions putting the stuff into the new table.
I've been thinking of parsing the _G table after new indexes and move those to the new table but I'm sure there are some lua-magicians out here that know a much cleaner, better working solution to this.
All this is in one C lua_state* , so if there are any solutions adding this loadAPI function in C/C++ and just registrating it at the state this would be fine, too.
I've also looked at "require", but didn't seem to understand whether it does what I need.
Using Lua 5.2.3
Hope i didn't forget anything.
Thanks in advance :)
~InDieTasten
Try this:
function loadAPI(f,g)
_G[g]=setmetatable({},{__index=_G})
loadfile(f,"bt",_G[g])()
end
loadAPI("example.lua", "externAPI")
print(externAPI.apiFunction("Test"))

Write a Method like Vector.at()

I would like to know how is that the .at() method works, like the one in the Vector class of the C++, a method that both ways returns and/or assign a value to the member of the array.
i don't know if with a macro i can do it, or declaring 2 method with same name... any help?
i have been trying to find and open the file of vector, to see how it was written, that specific method, but i have not found it.
(its for a different structure i am building, but i would like to access to them with only one method)
Example of what i mean.
vec.at(x) = value;
newValue = vec.at(x);
You have to return a reference to the value. So instead of
int at(int idx)
You just do
int& at(int idx)
References are very similar to pointers with the difference that you cannot and dont have to dereference them in order to manipulate the value they are referencing
There should be an easy way to open any source file within the IDE. I use this feature all of the time to review the Standard Library, especially when wanting to review the signature of the member functions of std::vector, std::map, std::list, etc., without having to open a browser window.
Hopefully the IDE used has something similar to "open file at cursor". Since the IDE is not known by the question content, a more generic procedure is presented.
1) Make sure the relevant header name is in a #include line. For example:
#include <vector>
2) Move the editor cursor so that the cursor is clearly shown under a section of the header file name (e.g. vector).
3) Within the IDE run the "open file at cursor" command.
4) The IDE should now show the file highlighted by the cursor in its own window / tab, and there is no need to manually navigate to the area where all header files are located on the development system.
5) Search the newly opened file as desired.

How to test that a file is left unchanged?

I'm testing a function that may modify a file. How do i test that it is unchanged in the cases where I want it to?
I don't want to check the content, because the file may have been overwritten with the same content, changing the modification time.
I can't really check the modification time, either. Since I like tests to be self-contained, the original file would be written just before the (non-)modification test, rendering the modification time unreliable.
You can use DI to mock your filewriter. This way you do not need the file at all, only check if the write function is called and you know if the file was modified.
I would split the function into two separate functions; the first decides whether the modification should be made, the second makes the notification. The second is only called if necessary. In pretend language:
function bool IsModificationRequired()
{
// return true or false based on your actual code
}
function void WriteFile()
{
new File().Write("file");
}
function void WriteIfModified()
{
if (IsModificationRequired())
WriteFile();
}
And test
Assert.IsTrue(IsModificationRequired());
Well assuming you are using a text file and reasonable size. Just hash the file content, if before modify and after modidfy hashcode is same then - it means the file content is not changed.
Here is the link to Algorithim Design Manual - Steve Skiena (Google Book Result)
Section 3.8
How can i convicne you that a file isn't changed ?