www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to import for mixin contents only.

reply Taylor Hillegeist <taylorh140 gmail.com> writes:
So i want bitfields for just a little bit. but i dont want its 
dependencies. How is it done. I have tried this. but it doesnt 
seem to work on gdc. :(

struct Color_t {
	static if(__ctfe){
		import std.bitmanip:bitfields;
	}
	mixin(bitfields!(
		uint, "R",    8,
		uint, "G",   8,
		uint, "B",    8,
		uint, "A", 8));
}
Mar 09 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Thursday, 10 March 2016 at 04:07:54 UTC, Taylor Hillegeist 
wrote:
 So i want bitfields for just a little bit. but i dont want its 
 dependencies. How is it done. I have tried this. but it doesnt 
 seem to work on gdc. :(

 struct Color_t {
 	static if(__ctfe){
 		import std.bitmanip:bitfields;
 	}
 	mixin(bitfields!(
 		uint, "R",    8,
 		uint, "G",   8,
 		uint, "B",    8,
 		uint, "A", 8));
 }
__ctfe is a runtime construct, not compile-time. It cannot be used with static if. More over, it's only available *inside* a function that is currently being executed in a compile-time context. It has no role outside of that. What problem are you trying to solve here? I mean, what is the problem with whatever dependencies std.bitmanip:bitfields has that makes you only want to import it during compilation?
Mar 09 2016
next sibling parent Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 04:56:52 UTC, Mike Parker wrote:
 On Thursday, 10 March 2016 at 04:07:54 UTC, Taylor Hillegeist 
 wrote:
 So i want bitfields for just a little bit. but i dont want its 
 dependencies. How is it done. I have tried this. but it doesnt 
 seem to work on gdc. :(

 struct Color_t {
 	static if(__ctfe){
 		import std.bitmanip:bitfields;
 	}
 	mixin(bitfields!(
 		uint, "R",    8,
 		uint, "G",   8,
 		uint, "B",    8,
 		uint, "A", 8));
 }
__ctfe is a runtime construct, not compile-time. It cannot be used with static if. More over, it's only available *inside* a function that is currently being executed in a compile-time context. It has no role outside of that. What problem are you trying to solve here? I mean, what is the problem with whatever dependencies std.bitmanip:bitfields has that makes you only want to import it during compilation?
I am running on a MKL25Z development board. The output of the mixin works fine. but the dependencies of the std.bitmanip:bitfields are quite extensive. including std.format..etc
Mar 09 2016
prev sibling parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 04:56:52 UTC, Mike Parker wrote:
 On Thursday, 10 March 2016 at 04:07:54 UTC, Taylor Hillegeist 
 wrote:
 So i want bitfields for just a little bit. but i dont want its 
 dependencies. How is it done. I have tried this. but it doesnt 
 seem to work on gdc. :(

 struct Color_t {
 	static if(__ctfe){
 		import std.bitmanip:bitfields;
 	}
 	mixin(bitfields!(
 		uint, "R",    8,
 		uint, "G",   8,
 		uint, "B",    8,
 		uint, "A", 8));
 }
__ctfe is a runtime construct, not compile-time. It cannot be used with static if. More over, it's only available *inside* a function that is currently being executed in a compile-time context. It has no role outside of that. What problem are you trying to solve here? I mean, what is the problem with whatever dependencies std.bitmanip:bitfields has that makes you only want to import it during compilation?
I feel like this should do what i want it too. but it doesn't. struct Color_t { static if(1==1){ import std.bitmanip:bitfields; immutable string item = bitfields!( uint, "R", 8, uint, "G", 8, uint, "B", 8, uint, "A", 8); } mixin(item); }
Mar 10 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Thursday, 10 March 2016 at 16:20:42 UTC, Taylor Hillegeist 
wrote:
 I feel like this should do what i want it too. but it doesn't.

 struct Color_t {
 	static if(1==1){
 		import std.bitmanip:bitfields;
 		immutable string item = bitfields!(		
 				uint, "R",    8,
 				uint, "G",   8,
 				uint, "B",    8,
 				uint, "A", 8);
 	}
 	mixin(item);
 }
I wonder if compiler is smart enaugh to undestand that dependency is not needed at runtime in this case: http://dpaste.dzfl.pl/fd3bc2a839a3
Mar 10 2016
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 16:51:32 UTC, Andrea Fontana wrote:
 On Thursday, 10 March 2016 at 16:20:42 UTC, Taylor Hillegeist 
 wrote:
 I feel like this should do what i want it too. but it doesn't.

 struct Color_t {
 	static if(1==1){
 		import std.bitmanip:bitfields;
 		immutable string item = bitfields!(		
 				uint, "R",    8,
 				uint, "G",   8,
 				uint, "B",    8,
 				uint, "A", 8);
 	}
 	mixin(item);
 }
I wonder if compiler is smart enaugh to undestand that dependency is not needed at runtime in this case: http://dpaste.dzfl.pl/fd3bc2a839a3
well the latest gdc isnt smart enough. immutable(string) BF(){ if(!__ctfe) assert(0); import std.bitmanip:bitfields; return bitfields!( uint, "R", 8, uint, "G", 8, uint, "B", 8, uint, "A", 8); } struct Color_t { mixin(BF()); } is a fair try as well. but neither work.
Mar 10 2016
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 17:05:26 UTC, Taylor Hillegeist 
wrote:
 On Thursday, 10 March 2016 at 16:51:32 UTC, Andrea Fontana 
 wrote:
 On Thursday, 10 March 2016 at 16:20:42 UTC, Taylor Hillegeist 
 wrote:
 [...]
I wonder if compiler is smart enaugh to undestand that dependency is not needed at runtime in this case: http://dpaste.dzfl.pl/fd3bc2a839a3
well the latest gdc isnt smart enough.
So interestingly this will work if i add -ffunction-sections to my compiler options? no idea why?
Mar 10 2016
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 17:22:58 UTC, Taylor Hillegeist 
wrote:
 On Thursday, 10 March 2016 at 17:05:26 UTC, Taylor Hillegeist 
 wrote:
 On Thursday, 10 March 2016 at 16:51:32 UTC, Andrea Fontana 
 wrote:
 On Thursday, 10 March 2016 at 16:20:42 UTC, Taylor Hillegeist 
 wrote:
 [...]
I wonder if compiler is smart enaugh to undestand that dependency is not needed at runtime in this case: http://dpaste.dzfl.pl/fd3bc2a839a3
well the latest gdc isnt smart enough.
So interestingly this will work if i add -ffunction-sections to my compiler options? no idea why?
However my binary goes from 4kb with static text to 11kb with the above change.
Mar 10 2016
parent reply Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 17:24:51 UTC, Taylor Hillegeist 
wrote:
 On Thursday, 10 March 2016 at 17:22:58 UTC, Taylor Hillegeist 
 wrote:
 On Thursday, 10 March 2016 at 17:05:26 UTC, Taylor Hillegeist 
 wrote:
 On Thursday, 10 March 2016 at 16:51:32 UTC, Andrea Fontana 
 wrote:
 On Thursday, 10 March 2016 at 16:20:42 UTC, Taylor 
 Hillegeist wrote:
 [...]
I wonder if compiler is smart enaugh to undestand that dependency is not needed at runtime in this case: http://dpaste.dzfl.pl/fd3bc2a839a3
well the latest gdc isnt smart enough.
So interestingly this will work if i add -ffunction-sections to my compiler options? no idea why?
However my binary goes from 4kb with static text to 11kb with the above change.
I suppose the linker optimized the functions away since they are now in their own section. But it seems a hacky way to do this.
Mar 10 2016
parent reply Andrea Fontana <nospam example.com> writes:
On Thursday, 10 March 2016 at 17:43:08 UTC, Taylor Hillegeist 
wrote:
 I suppose the linker optimized the functions away since they 
 are now in their own section. But it seems a hacky way to do 
 this.
AFAIK assert(0) and other falsey assert have a special meaning for compiler. So probably it's not so hacky but just a way to say that case can't happen. It is used also for: auto myfunc(int i) { if (i == 0) return 10; else if (i == 1) return 3; assert(0); } And also with non final switch using switch(var) { case ... ... default: assert(0); }
Mar 10 2016
parent Taylor Hillegeist <taylorh140 gmail.com> writes:
On Thursday, 10 March 2016 at 22:07:23 UTC, Andrea Fontana wrote:
 On Thursday, 10 March 2016 at 17:43:08 UTC, Taylor Hillegeist 
 wrote:
 I suppose the linker optimized the functions away since they 
 are now in their own section. But it seems a hacky way to do 
 this.
AFAIK assert(0) and other falsey assert have a special meaning for compiler. So probably it's not so hacky but just a way to say that case can't happen. It is used also for: auto myfunc(int i) { if (i == 0) return 10; else if (i == 1) return 3; assert(0); } And also with non final switch using switch(var) { case ... ... default: assert(0); }
I'm good with the assert(0), I'm not so happy with the linker not being able to create a binary without the -ffunction-sections option. I think there needs to be a ctimport keyword or something. I should not have to deal with the imports only used during compile time. it is not a good thing.
Mar 10 2016