Initialiser element is not a compile time constant - iphone

In my constant file, I have included the below line
NSString * ALERT_OK = NSLocalizedString(#"Ok",#"Ok");
After this, when I tried to compile I am receiving the below error
Initialiser element is not a compile time constant
How can I debug this?

The problem is that NSLocalizedString is a function which returns different values, depending on the language. It is not a constant which can be figured out until the system is running.
Instead, use:
#define ALERT_OK NSLocalizedString(#"Ok",#"Ok");
And it will now simply replace ALERT_OK with the function and you will be fine. (Note that you should be using some kind of prefix to all global values like this so that you don't accidentally create something with the same name being used somewhere else.)

Related

define help for variable in Matlab

In Matlab, it is easy to generate "help" for a function, as follows.
function out = foo()
% helpful information about foo
end
When we execute help foo, we get "helpful information about foo".
However, suppose we would like to define help for a variable, probably as a definition. How could we do such a thing? It would be nice if we could do something like
x = 3; % m ... position
help x
and get "m ... position". However, I don't believe such functionality exists.
The only reasonable way I see around this is to define every variable as a struct with keys value and description.
x.value = 3;
x.description = 'm/s ... position';
This requires we define every variable as a struct, which is kind of annoying and, I worry (should I?), unperformant (it's simulation code and these variables are accessed repeatedly).
Is there another solution I'm not considering? Should I be worried about making every variable a struct?
Your code should be self-documenting. Instead of variable name x, use position.
Furthermore, all variables should be local, so you can easily look for its definition (with comment) within the function you are editing.
Variables declared further away (with larger scope within the function) should have longer, more self-explanatory names than variables with a smaller scope (e.g. use within a short loop.
There are only two three cases where variables are declared outside the function’s scope:
Class properties. You can actually document these.
In a script, you have access to variables that already existed before the script started. A good reason not to use scripts or depend on the base namespace in larger projects.
Global variables. You should never use global variables for many reasons. Just don’t.

How to ignore a return value in Common Lisp

I'm working with some code which calls ADJUST-ARRAY. I am getting a warning message from the Lisp interpreter (CMUCL) that the return value of ADJUST-ARRAY should not be ignored.
In the code I am working on, ADJUST-ARRAY modifies its argument in place, if I am not mistaken. So it's not necessary to do anything with the return value. Is there a designated way to ignore a return value in Common Lisp? Of course, I could assign the return value to some variable, and then ignore the variable. But that feels clumsy.
I could also assign the return value to the ADJUST-ARRAY argument, something like:
(setq my-array (adjust-array my-array ...))
but that seems to suggest that I'm not sure if ADJUST-ARRAY will modify MY-ARRAY in place.
Any advice is welcome, thanks in advance.
You are correct. As the documentation states:
The result is an array of the same type and rank as array, that is
either the modified array, or a newly created array to which array
can be displaced, and that has the given new-dimensions.
If the result is a newly created array then of course the function would have had no effect on the argument.
Common Lisp almost always require you to use the returned value rather than old bindings in order to have portable code.
The specification of adjust-array is that the adjusted array is the one returned.
What you can expect of the argument array afterwards to be is a bit complicated and may differ between implementations in some cases.
Just use the one returned. You might use setf to modify or let to create a binding.

Define style in HAL library

In HAL library used in the STM32 code we see that a style like this is used:
#define I2C1 ((I2C_TypeDef *) I2C1_Base)
Why define is used like this?
Why pointer type is used?
Why isn't the star outside the parentheses?
I2C1_Base is declared elsewhere, and is a number as a hex literal.
(I2C_TypeDef *) casts the I2C1_Base number to a pointer to an I2C_TypeDef, the star needs to be within the parenthesis as it's casting to a pointer type, if it were outside then it would be a dereference and wouldn't compile.
The define is there to allow code like
I2C1->someRegister = 0xf00;
rather than needing to manually type the cast each time.

how single and double type variables work in the same copy of code in Matlab like template in C++

I am writing a signal processing program using matlab. I know there are two types of float-pointing variables, single and double. Considering the memory usage, I want my code to work with only single type variable when the system's memory is not large, while it can also be adapted to work with double type variables when necessary, without significant modification (simple and light modification before running is OK, i.e., I don't need runtime-check technique). I know this can be done by macro in C and by template in C++. I don't find practical techniques which can do this in matlab. Do you have any experience with this?
I have a simple idea that I define a global string containing "single" or "double", then I pass this string to any memory allocation method called in my code to indicate what type I need. I think this can work, I just want to know which technique you guys use and is widely accepted.
I cannot see how a template would help here. The type of c++ templates are still determined in compile time (std::vector vec ...). Also note that Matlab defines all variables as double by default unless something else is stated. You basically want runtime checks for your code. I can think of one solution as using a function with a persistent variable. The variable is set once per run. When you generate variables you would then have to generate all variables you want to have as float through this function. This will slow down assignment though, since you have to call a function to assign variables.
This example is somehow an implementation of the singleton pattern (but not exactly). The persistent variable type is set at the first use and cannot change later in the program (assuming that you do not do anything stupid as clearing the variable explicitly). I would recommend to go for hardcoding single in case performance is an issue, instead of having runtime checks or assignment functions or classes or what you can come up with.
function c = assignFloat(a,b)
persistent type;
if (isempty(type) & nargin==2)
type = b;
elseif (isempty(type))
type = 'single';
% elseif(nargin==2), error('Do not set twice!') % Optional code, imo unnecessary.
end
if (strcmp(type,'single'))
c = single(a);
return;
end
c = double(a);
end

Replace a macro with a different definition in Eclipse?

I'm working on a project which defines globals like this:
// Define an correctly-sized array of pointers to avoid static initialization.
// Use an array of pointers instead of an array of char in case there is some alignment issue.
#define DEFINE_GLOBAL(type, name, ...) \
void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
Which apparently works fine, but causes Eclipse to show every single usage of one of these globals as an error.
I would prefer that it be this:
#define DEFINE_GLOBAL(type, name, ...) \
type name;
But I can't change this file, so is there a way to tell Eclipse to pretend that that's the macro's definition?
If you #define the preferred definition after the initial (unwanted) definition, Eclipse seems to use the most recent definition when it does the dynamic macro expansion.
Thus, if you re-#define the macro in the file you are editing, this may solve your problem.
Granted that this is a kludge and may cause unforeseen problems, it may work for your implementation.