https://stackoverflow.com/a/6044657/1165790
I was under the impression that there are two ways one can assign/call things:
1) by value (the actual bit encoding of a type is assigned/sent to a variable/function and 2) by reference (the memory address of the data's location is assigned/sent to a variable/function.
What exactly is passing a 'reference by value'?
What it means is that you pass the reference (essentially a pointer abstraction) the same way you would pass any other primitive: by value.
Passed by Value Passed by reference
--------------- -------------------
Integer Object
Reference Object that the reference points to
Because references are passed by value, you get the same behavior as you would with
anything else that is passed by value; namely, that the function to which you're passing the reference uses a copy of the reference, and not the actual reference.
So if you change the reference within the function, the original reference outside the function does not change.
This is wordplay. A reference is the memory address, or, strictly speaking, an abstraction that has all the attributes of a memory address. So to pass a reference by value is just what you said: the bits of the address are passed to the callee. In the more general way of speaking that most computer language texts use, this is exactly the same as saying the object is passed by reference.
Related
I was creating a function and I tried to increase the value of the parameter which was declared as Integer. But it said that the value of that parameter can not be reassigned. I am a beginner so if I am missing some concept please tell me.
Yes, in Kotlin function parameters can't be changed.
(However, if they refer to mutable objects, then those objects can change. For example, if you declared fun a(b: List<String>), then you could add another String to the List; but you couldn't set b to refer to a different List.)
This is different from Java, where parameters are variable (unless specified as final).
This behaviour was announced in Kotlin milestone M5.1, where it was explained as avoiding confusion (especially in constructors), and promoting good style.
See also this answer.
Is there a term for this technique? One prominent example is the WinAPI: SendMessage( hwnd, msg, info1, info2 ) where parameters #3 and #4 only make sense per msg (which also means there are cases when only one or none of those two parameters are needed). See MSDN.
Rephrased: having an all-purpose function that always accepts multiple arguments, but interpreting them depends on a previous argument. I don't want to talk about open arrays, open arguments, typeless arguments... I know all that. That's not what I'm asking - I want to have the term for this type of functions (any maybe also how unspecific parameters are called).
This is not about casts or passing by reference - the parameter types are always the same. Other example: calculate( char operation, int a, int b ) which is then used as
calculate( '+', 2, 5 ) (parameters #2 and #3 are summands)
calculate( '/', 4, 2 ) (parameter #2 is the divident and parameter #3 is the divisor)
calculate( '!', 3, 0 ) (parameter #2 is the factorial and parameter #3 is unused)
In all these cases the data type is always the same and never casted. But the meaning of parameters #2 and #3 differ per parameter #1. And since this is the case it is difficult to give those parameters a meaningful name. Of course the function itself most likely uses a switch(), but that is not subject to my question. How are parameters #2 and #3 called, where a distinct name cannot be found, but data types are always the same?
The fact the msg argument "changes" the parameters is through a simple switch statement. Each "msg" in the switch knows the parameters(with type) needed and casts them appropriately.
This "technique" is called passing by reference, or passing by address. The latter is usually used for method pointers.
There is no Special name if that is what you are asking. It is a regular function, method or procedure.
The referenced Function is a Win32 API Function, which may be referred to as a "Windows function call."
This is an example of a static Parameter and multiple Dynamic parameters.
The static is the "msg" and the dynamic is described as the following:
These parameters are generic pointers. Passed by reference. They can point to any data type or no value, ie null pointer. It is up to the sender to lock the memory in place, and the receiving method to interpret the pointer correctly (through pointer casts).
This is an example of typeless argument passing. The only thing passed is a memory address. It is dangerous since the types passed must be agreed upon ahead of time(by convention and not contract as with a typed language construct) and must match on both sides of the call.
This was common before C++, in the C days, we only had C structs to pass around. Leading to many General Fault Protection errors. Since then, typed interfaces mostly have replaced the generic equivalents through libraries. But the underlying Win32 methods remain the same. The main substantial change since its' inception is the acceptance of 64-bit pointers.
Although not widely supported, what you are referring to would be a dependently typed function (or dependently typed parameters).
To quote wikipedia on dependent types
A "pair of integers" is a type. A "pair of integers where the second is greater than the first" is a dependent type because of the dependence on the value.
The parameters could have a type that depends on a value. The type of info1 depends on the value msg as does info2.
In order to make this approach work in a language without dependent types, the dependent parameters are given a very generic type that is only refined later on when more information is available. When the type of msg becomes known (at runtime) only then is are the types of info1 and info2 assumed. Even though the language doesn't allow you to express this dependency, I would still call the approach a dependently type one.
I am currently creating a workflow in which the user captures a point geometry by clicking on the map (this works), then the map zooms to the point's extent (this also works), and then buffers the point (this does not work).
My BufferTask activity gives me this: "Unhandled exception: 'Required parameter is null or empty. Parameter name: Geometry.SpatialReference in activity '1.3: BufferTask'"
This doesn't make sense to me since I have indeed entered a value for this parameter.
sidenote: Geocortex's documentation is virtually non-existent. My inner-cynic is telling me that this is on purpose so that you keep paying them to do things for you.
My 2 best guesses would be either that the buffer spatial reference is null (the spatial reference used to actually create the buffer itself), or that the selectedLocation spatial reference is null for some reason.
For the first, see if there's a value entered for "Buffer Spatial Reference" which you'll see in the properties panel on the right side of the designer when you've got the Buffer Task selected. Note that using Web Mercator as the Buffer Spatial Reference will likely give you an inaccurate buffer due to distance distortion in that projection.
For the second, you could explicitly assign a SpatialReference to selectedLocation.SpatialReference using an assign activity (making sure that the assigned SpatialReference matches the actual spatial reference of your location).
Check to make sure you specified a correct spatialReference, so the workflow module can assess the "length" of buffer.
In addition isolate the problem.
Create a clean variable of type spatialReference and assign a NEW SpatialRef(wkid). Then use that variable.
True or False: When a function is called, the calling program suspends until
the function completes.
True or False: When you call a function with a list as a parameter, you could
change the original calling program’s list from within the function
True or False: When you call a function with a dictionary as a parameter, you could change the original calling program’s dictionary from within the
function.
Im very fuzzy on these questions and what they mean, could someone help explain these?
1.True
the original program will stop and going to run the function. It will back to the place where it stop and start to execute the next line command after function done.
2.False
call by value. it wont change the original variable's value.
3.True
call by pointer or call by reference.
Sometime yes and sometimes no. It depends whether you are calling it synchronously or asynchronously, see this answer for the distinction: Asynchronous vs synchronous execution, what does it really mean?
In most cases yes, because you are just passing by reference, i.e you are passing the location in memory of the param you are passing. However, you can also pass by value, i.e passing a copy of the object. See this question for more detail: What's the difference between passing by reference vs. passing by value?
same answer as your previous question. Whether you are passing an integer, string, array, list, dictionary, it doesn't matter, it all depends on whether you are passing by reference or by value. Which one of these happens by default depends on which programming language you use. You can determine which one is happening pretty easily with some experimentation: Define a function with a dictionary variable, add a key/value pair call another function with the dict as a param, and modify it in the called function, then print it out in the calling function once the called function has returned. If it has been modified, you know you are passing by reference. If it has the original key/value you set in the caller and is not modified, you know you are passing by value.
What is the advantage of having this/self/me pointer mandatory explicit?
According to OOP theory a method is supposed to operate mainly (only?) on member variables and method's arguments. Following this, it should be easier to refer to member variables than to external (from the object's side of view) variables... Explicit this makes it more verbose thus harder to refer to member variables than to external ones. This seems counter intuitive to me.
In addition to member variables and method parameters you also have local variables. One of the most important things about the object is its internal state. Explicit member variable dereferencing makes it very clear where you are referencing that state and where you are modifying that state.
For instance, if you have code like:
someMethod(some, parameters) {
... a segment of code
foo = 42;
... another segment of code
}
when quickly browsing through it, you have to have a mental model of the variables defined in the preceding segment to know if it's just a temporary variable or does it mutate the objects state. Whereas this.foo = 42 makes it obvious that the objects state is mutated. And if explicit dereferencing is exclusively used, you can be sure that the variable is temporary in the opposite case.
Shorter, well factored methods make it a bit less important, but still, long term understandability trumps a little convenience while writing the code.
You need it to pass the pointer/reference to the current object elsewhere or to protect against self-assignment in an assignment operator.
What if the arguments to a method have the same name as the member variables? Then you can use this.x = x for example. Where this.x is the member variable and x is the method argument.
That's just one (trivial) example.
I generally use this (in C++) only when I am writing the assignment operator or the copy constructor as it helps in clearly identifying the variables. Other place where I can think of using it is if your function parameter variable names are same as your member variable names or I want to kill my object using delete this.
Eg would be where member names are same as those passed to method
public void SetScreenTemplate(long screenTemplateID, string screenTemplateName, bool isDefault)
{
this.screenTemplateID = screenTemplateID;
this.screenTemplateName = screenTemplateName;
this.isDefault = isDefault;
}