when i write var x = 0
I know that x is an object that has properties and methods (created from Int Structure).
Where and how does x store the 0 ?
Is the 0 stored as a property of x ?
If yes, what would be the type of that property ?
If not, where is it stored ?
x is not an object, strictly speaking. "Object" is a name we give to instances of classes, not structs. x is an instance of the Int struct.
The Int structure wraps a Builtin integer type, and defines a bunch of methods you can call on it. That builtin integer literal type isn't accessible from Swift (nor is there a reason for it to be). Like all structures, instances of Int are stored on the runtime stack. They're not objects on the heap like Integer in Java, for example.
You can see the implementation details of (U)Int(8/16/32/64) here. This file uses the Generate Your Boilerplate (GYB) preprocessor created by the Swift team to generate .swift files from .swift.gyb template files.
On line 221, you can see the property _value of type Builtin.${BuiltinName}. The GYB preprocessor expands this out so that Int has a _value of type Builtin.Int, Int64 has Built.Int64, etc.
Related
How does one pass a Simulink.Parameter structure (which, in my case, is a structure of structures) to a C S-function?
Edit:
Information on Simulink.Parameter
You can create a Simulink.Parameter object this way:
modelParameters = Simulink.Parameter;
modelParameters.Value = myStruct;
modelParameters.CoderInfo.StorageClass = 'ExportedGlobal';
The myStruct value is a regular matlab structure of structures. This is how it looks in my case:
This is a special object type for passing parameters to Simulink and I am looking for a mechanism to access it from a C S-function.
Download a MnWE from here.
Edit 2:
I read the parameters this way:
modelParameters_T *modelParameters = (modelParameters_T*)mxGetPr(ssGetSFcnParam(S, PARAM_STRUCT));
But I can see why this approach doesn't work - the structure object from Matlab is not similar to a C structure, i.e. is not contiguous in memory and contains other properties too. I think I will cast the Matlab structure to an array and then cast the array in C to my struct definition.
mxGetPr is not the right way to access your parameter which is an object type. It is not a struct type. Even if it is a struct type you need to use mxArray API to access struct fields. You need to use something like the following code to access the fields.
mxArray* param = ssGetSFcnParam(S, PARAM_STRUCT);
mxArray* prop = mxGetProperty(param, 0, "Value"); // Get Value property from param object
// If prop is double precision use the following line to get its value
double* prop = *(mxGetPr(prop));
Check out mxArray API in the doc for accessing different types of mxArrays.
I have a variable called 'int' with alot of data in it. I would like to find a way to programically rename this variable with a user input. So I can query the user indentifcation information about the data, say the response is 'AA1', I want either rename the variable 'int' to 'AA1' or make 'AA1' a variable that is identical to int.
A problem using the input command arises because it allows the user to assign a value to an already created varialbe, instead of actually creating a variable name. Would using the eval function, or a variation of it, help me achieve this? Or is there an easier way?
Thanks!
Just for the record, int is a rather poor variable name choice.
That aside, you can do what you want as follows
say foo is the variable that holds a string that the user input. You can do the following:
% eliminate leading/trailing whitespace
foo = strtrim(foo);
a = regexp('[a-zA-Z][a-zA-Z0-9_]*',foo));
if numel(a) == 0
fprintf('Sorry, %s is not a valid variable name in matlab\n', foo);
elseif a ~= 1
fprintf('Sorry, %s is not a valid variable name in matlab\n', foo);
elseif 2 == exist(foo,'var')
fprintf('Sorry, %s already in use as a variable name.');
else
eval([foo,' = int']);
end
Assuming int (and now foo) is a structure with field named bar, you can read bar as follows:
barVal = eval([foo,'.bar']);
This is all somewhat clunky.
An alternative approach, that is far less clunky is to use an associative array, and let the user store various values of int in the array. The Matlab approach for associative arrays is Maps. That would be my preferred approach to this problem. Here is an example using the same variables as above.
nameValueMap = containers.Map;
nameValueMap(foo) = int;
The above creates the association between the name stored in foo with the data in the variable int.
To get at the data, you just do the following:
intValue = nameValueMap(foo);
I have 2 different classes but same primitive type declaration as you see below
int x = 0;
- (void)viewDidLoad{
[super viewDidLoad];
}
if I change one of them name "x" to "y" there is no error? WHY? seperate classes same variable whats the problem???
That's because the variable x is shared among classes. I think (but never tried) if you declare extern int x in another file, you could share the x variable.
Try static int x = 0. In general, always declare an internal class variable as static unless you want to share it with another file.
I meet the question. As Says , because the variable x is shared among classes. Modify the variable x to another name.
How do I detect whether a variable is float, double, int, etc.?
Thanks.
Objective-C is not like PHP or other interpreted languages where the 'type' of a variable can change according to how you use it. All variables are set to a fixed type when they are declared and this cannot be changed. Since the type is defined at compile time, there is no need to query the type of a variable at run-time.
For example:
float var1; // var1 is a float and can't be any other type
int var2; // var2 is an int and can't be any other type
NSString* var3; // var3 is a pointer to a NSString object and can't be any other type
The type is specified before the variable name, also in functions:
- (void)initWithValue:(float)param1 andName:(NSString*)param2
{
// param1 is a float
// param2 is a pointer to a NSString object
}
So as you can see, the type is fixed when the variable is declared (also you will notice that all variables must be declared, i.e. you cannot just suddenly start using a new variable name unless you've declared it first).
In a compiled C based language (outside of debug mode with symbols), you can't actually "detect" any variable unless you know the type, or maybe guess a type and get lucky.
So normally, you know and declare the type before any variable reference.
Without type information, the best you can do might be to dereference a pointer to random unknown bits/bytes in memory, and hopefully not crash on an illegal memory reference.
Added comment:
If you know the type is a legal Objective C object, then you might be able to query the runtime for additional information about the class, etc. But not for ints, doubles, etc.
Use sizeof. For double it will be 8. It is 4 for float.
double x = 3.1415;
float y = 3.1415f;
printf("size of x is %d\n", sizeof(x));
printf("size of y is %d\n", sizeof(y));
I am wraping some SQL Stored Procs with in/out parameters. This of course means I have to do ugly stuff like declare my parameters as by reference and use mutables.
How would I do this in F#?
F# does indeed have a byref parameter. Here's an example from the MSDN page:
type Incrementor(z) =
member this.Increment(i : int byref) =
i <- i + z
Mutable variables also exist (though there's an important difference between using ref and mutable variables, either of which can be used for many of the same purposes). The MSDN page on this subject is very informative - including a discussion of when to use which keyword/construct.
Example of reference variables:
// Declare a reference.
let refVar = ref 6
// Change the value referred to by the reference.
refVar := 50
Example of mutable variables:
// Declare a reference.
let mutable refVar = 6
// Change the value referred to by the reference.
refVar <- 50
As you can see, the syntax for assignment (as well as retrieval) differs between the two constructs, too.