If I have a .less file that imports two other .less files that both use and define the same variable name, the last definition in whichever file was imported last is the value used in all the other files. For example:
#import (less) ex1.less
#import (less) ex2.less
ex1.less:
#var: classname;
.#{var} {/*css*/}
ex2.less:
#var: classname2;
.#{var} {/*css*/}
generates:
.classname2 {/*css*/}
.classname2 {/*css*/}
I am not sure how to use namespaces here, so any help would be very appreciated.
Much like CSS Less cascades it's values, so the last var declaration wins.
You only import one of the files.
Change the second var's name.
Or make one of your less files more concise and use the var where needed.
Related
First of all I would like to mention this question: Customize bootstrap 4 variables using it's own variables?, where it seems the same question has been asked but without fruitful answer so I thought I would ask again with a very specific example.
I basically want to overwrite some of bootstrap 4's default variables. In the example below I want to overwrite $font-size-base. I followed the recommended approach of the bootstrap documentation but it only seems to be partially working. Bootstrap's default variables (in _variables.scss) reuse other defined variables. For instance : $input-btn-font-size: $font-size-base !default;. When changing $font-size-base with the approach below however, it doesn't affect $input-btn-font-size.
#import "node_modules/bootstrap/scss/functions";
#import "node_modules/bootstrap/scss/variables";
$font-size-base: 0.9rem;
$secondary: $gray-400;
#import "node_modules/bootstrap/scss/bootstrap";
On the other hand if I use the next approach where I import the variables later, then I do get the desired result where all variables that depend on $font-size-base get changed as well. This however is not the recommended approach and becomes clear when you want to define a variable that depends on a default variable. See $secondary: $gray-400;, this then throws a compile error given that $gray-400 is unknown.
#import "node_modules/bootstrap/scss/functions";
$font-size-base: 0.9rem;
$secondary: $gray-400;
#import "node_modules/bootstrap/scss/variables";
#import "node_modules/bootstrap/scss/bootstrap";
What is the recommended solution here?
Separate the override variables....
The only overrides that need to follow the /variables import are those that reference variables (ie: $secondary: $gray-400;).
If you just want to override a variable that doesn't reference another (ie: $font-size-base: 2.9rem;) that would go before the /variables import...
/* simple overrides */
$font-size-base: 2.9rem;
#import "bootstrap/functions";
#import "bootstrap/variables";
/* var dependent overrides */
$theme-colors: (
secondary: $gray-400
);
#import "bootstrap";
https://www.codeply.com/go/wpc3XKQ8TG
Note: To override a theme color such as secondary you must update the theme-colors map.
Right now I am trying to validate, that file doesn't have "violating" imports. As far as i get it preprocessor #include #import macro won't be listed listed in ASTContext and SourceManager (after all they are macro), while you certainly can track ImportDecl during AST traversal.
So the only option left right now to get list of imports is to get files source code and use regular expressions? Maybe there are other "proper" ways?
So basically you can iterate on import declarations using ASTContext - local_imports
I would like to use ProGuard to just rename variable and functions. I want the resultant class files to be identical to the source jar in every regard except for the names. Is there an option to do this?
My attempts at using dontoptimize and dontshrink are not working.
Just put the following in your .pro file, and make sure that you don't use the "-keep" option to prevent other things from being obfuscated.
# Don't keep the local variables attributes (LocalVariableTable and LocalVariableTypeTable are dropped).
-keepattributes Exceptions,Signature,Deprecated,SourceFile,SourceDir,LineNumberTable,Synthetic,EnclosingMethod,RuntimeVisibleAnnotations,RuntimeInvisibleAnnotations,RuntimeVisibleParameterAnnotations,RuntimeInvisibleParameterAnnotations,AnnotationDefault,InnerClasses,*Annotation*
In other words, make sure that you don't have the following in your options
-keepattributes LocalVariableTable,LocalVariableTypeTable
Using objective c , I have 2 classes that are using hardware, and are written in c +objC .
The other classes in the project are objective c, and they create instance of those classes.
My question .
Lets say I have classA.m and classB.m . they both have an integer const that needs to be the same say : const int numOfSamples=7;
I am seeking for the best solution to create some configuration file, that will holds all this const variables, that both A and B can see them .
I know some ways,but I wonder whats the RIGHT thing to do .
I wonder if I can just create a : configuration.m and write them into it .
to use a singleton file that holds all globals .
Number 1 seems to me the best , but how exactly should I do it ?
Thanks.
For approach 1 to work, you need to define two files:
a header file where you declare all of your constants;
a .m file where your constants are defined and initialized.
In your example:
/* .h file */
extern const int numOfSamples;
/* .m or .c file */
const int numOfSamples = 7;
Then, you include the .h header in every other file where you need those constants. Notice the extern keyword, this will declare the variable without also defining; in this way, you can include the .h file multiple times without having a duplicate symbol error.
EDIT:
The approach I suggest is the correct way to handle global variables in a C program.
Now, if global variables are a good thing or not, well, that is a longer story.
Generally speaking, global variables are tricky and go against a 40 years long effort towards better encapsulation (aka, information hiding) of data and behavior in a program (see "On the Criteria to Be Used in Decomposing Systems Into Modules", David Parnas, 1972).
To further explain this, one aspect of the problem is exactly what you mention in your comment: the possibility of one module changing the value of a global variable and thus affecting the whole behavior of the program. This is recognizedly bad and leads to uncontrollable side effects (in any non trivially sized program).
In your case, I think things are a bit different, in that you are talking about "configuration" and "const" values. This is an altogether different case than the general one and I think you could safely use a header file of consts to that aim.
That said, you understand that the whole theme of configuration is a huge one, in general. E.g., you could need mechanisms to change your program configuration on the fly; in this case the constant variable header approach would be not correct. Or, your program configuration could depend on the state of some remote system (imagine: you are logged in vs. you are not logged in).
I can't guarantee that using a header file is the best approach for your case, but I hope that the above discussion and the example I gave you can help you figure that out.
I think the best way is to use a plist file with all your configuration values.
If you have few configuration values, you can use the Info.plist file.
In the header of the class, outside of interface declaration, I've declared global constants:
NSString * const gotFilePathNotification = #"gotFilePath";
NSString * const gotResultNotification = #"gotResultOfType";
gotResultNotification is used only in this class (yet), but I reference gotFilePathNotificaion in another class implementation. To do it, I import this header.
When I try to compile, I get a duplicate symbol linker error about gotFilePathNotification in this header. Why does it happen?
You have two identifier(s) with same name across two different compilation unit(s) at file scope. This violates One Definition Rule. Instead you need to -
Declare the global variables marking to have external linkage in a header file.
extern NSString * const gotFilePathNotification;
Now provide the definition in only one source file.
NSString * const gotFilePathNotification = #"gotFilePath";
Now where ever you need to use these variables, include the header in the source file.
You need to declare them extern in the header file and define them in implementation file. See this question for clarification. Global Variables in Cocoa/Objective-C? .
The second response provides the clarification that I will reiterate here. The default storage qualifier for variables is static. This means when you try to link two different files with the same variable, as will happen when you import your header file, the linker will construe that the variable is multiply-defined.
Also make sure you're including the h file and not the m file. This was driving me nuts.