digitalmars.D.learn - use template function without assignment
- JS (19/19) Jul 29 2013 I have created a template Pragma that emulates pragma but better,
- Meta (7/27) Jul 29 2013 Does this code do what you want, or are there other requirements
- Meta (13/19) Jul 29 2013 Actually, sorry, that's the exact same code, just with some
- JS (24/45) Jul 30 2013 If I use enum or alias they both have the same problem(The
- Meta (20/39) Jul 30 2013 Can you post some more code that exhibits exactly the behaviour
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (17/20) Jul 30 2013 Ditto.
- JS (22/44) Jul 30 2013 I already stated why this is not a proper example, I'm not using
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (16/37) Jul 30 2013 Thank you. Now we have something to work on. The program above produces
- Namespace (20/40) Jul 30 2013 You could call your template in a static CTor:
- JS (10/58) Jul 30 2013 This is useless. It's goal is to debug templates and essentially
I have created a template Pragma that emulates pragma but better, the problem is that I have to assign it to something which is very redundant in my code: enum temp = Pragma!(msg) e.g., template Pragma(alias amsg) { string Pragma(string file = __FILE__) { pragma(msg, amsg); return ""; } } When I try to use void instead of string and do something like Pragma!(msg) I get an error that the template has no effect. It does have an effect but what it is complaining about is exactly what I want. I've tried all kinds of combinations(mixins work but I then can't ise __FILE__) and nothing works. Maybe someone has an idea.
Jul 29 2013
On Monday, 29 July 2013 at 23:09:20 UTC, JS wrote:I have created a template Pragma that emulates pragma but better, the problem is that I have to assign it to something which is very redundant in my code: enum temp = Pragma!(msg) e.g., template Pragma(alias amsg) { string Pragma(string file = __FILE__) { pragma(msg, amsg); return ""; } } When I try to use void instead of string and do something like Pragma!(msg) I get an error that the template has no effect. It does have an effect but what it is complaining about is exactly what I want. I've tried all kinds of combinations(mixins work but I then can't ise __FILE__) and nothing works. Maybe someone has an idea.Does this code do what you want, or are there other requirements as well? void Pragma(alias amsg)(string file = __FILE__) { pragma(msg, amsg); }
Jul 29 2013
On Tuesday, 30 July 2013 at 01:06:39 UTC, Meta wrote:Does this code do what you want, or are there other requirements as well? void Pragma(alias amsg)(string file = __FILE__) { pragma(msg, amsg); }Actually, sorry, that's the exact same code, just with some syntactic sugar. To avoid the "expression has no effect" problem, you could use an alias instead of an enum. You're getting the error when you try to use pragma in the "global" scope, right? void Pragma(alias amsg)(string file = __FILE__) { pragma(msg, amsg); } alias temp = Pragma!("test"); In general, D doesn't have top-level expressions like what I think you want to do here. Maybe somebody else knows a way to work around that.
Jul 29 2013
On Tuesday, 30 July 2013 at 01:19:23 UTC, Meta wrote:On Tuesday, 30 July 2013 at 01:06:39 UTC, Meta wrote:If I use enum or alias they both have the same problem(The annoying mandatory assignment). I see the reason why it does it but I don't know it because in this case I do not want an effect. (pragma has no "effect" which is what I'm trying to emulate... works well except for that annoying quirk of having to assign it to something). BTW, is void Pragma(alias amsg)(string file = __FILE__) short for template Pragma(alias amsg) { void Pragma(string file = __FILE__) or is there some real difference? I could potential do something like template Group(alias G1) { void Group(alias G2)(int s) { writeln(s); } } Group!("x")("y")(3); // doesn't work, Group!("x")!("y")(3); // doesn't workDoes this code do what you want, or are there other requirements as well? void Pragma(alias amsg)(string file = __FILE__) { pragma(msg, amsg); }Actually, sorry, that's the exact same code, just with some syntactic sugar. To avoid the "expression has no effect" problem, you could use an alias instead of an enum. You're getting the error when you try to use pragma in the "global" scope, right? void Pragma(alias amsg)(string file = __FILE__) { pragma(msg, amsg); } alias temp = Pragma!("test"); In general, D doesn't have top-level expressions like what I think you want to do here. Maybe somebody else knows a way to work around that.
Jul 30 2013
On Tuesday, 30 July 2013 at 07:02:51 UTC, JS wrote:If I use enum or alias they both have the same problem(The annoying mandatory assignment).Can you post some more code that exhibits exactly the behaviour you're describing? I can't replicate the problem you're having with the code you provided (nor the code *I* provided).BTW, is void Pragma(alias amsg)(string file = __FILE__) short for template Pragma(alias amsg) { void Pragma(string file = __FILE__) or is there some real difference?They're semantically equivalent. The first form is just shorthand for the second.I could potential do something like template Group(alias G1) { void Group(alias G2)(int s) { writeln(s); } } Group!("x")("y")(3); // doesn't work, Group!("x")!("y")(3); // doesn't workWhat you're doing here is pretty weird. That code is equivalent to: template Group(alias G1) { template Group(alias G2) { void Group(int s) { writeln(s); } } } And I really have no idea how it *should* behave. It definitely doesn't work on dpaste.dzfl.pl.
Jul 30 2013
On 07/30/2013 06:16 AM, Meta wrote:Can you post some more code that exhibits exactly the behaviour you're describing? I can't replicate the problem you're having with the code you provided (nor the code *I* provided).Ditto. JS, I wrote the following code for you. Could you please start with it and show us the problem that we are trying to solve. template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } void main() { enum msg = "hello"; Pragma!msg; } Ali
Jul 30 2013
On Tuesday, 30 July 2013 at 18:38:23 UTC, Ali Çehreli wrote:On 07/30/2013 06:16 AM, Meta wrote:I already stated why this is not a proper example, I'm not using Pragma in run time code(for lack of a better term). module main; import std.stdio; template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } template t() { Pragma!("help, this does not work!!!!!!!!!"); } void main() { enum msg = "hello"; Pragma!msg; t!(); }Can you post some more code that exhibits exactly thebehaviour you'redescribing? I can't replicate the problem you're having withthe codeyou provided (nor the code *I* provided).Ditto. JS, I wrote the following code for you. Could you please start with it and show us the problem that we are trying to solve. template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } void main() { enum msg = "hello"; Pragma!msg; } Ali
Jul 30 2013
On 07/30/2013 12:09 PM, JS wrote:I already stated why this is not a proper example, I'm not using Pragma in run time code(for lack of a better term). module main; import std.stdio; template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } template t() { Pragma!("help, this does not work!!!!!!!!!"); } void main() { enum msg = "hello"; Pragma!msg; t!(); }Thank you. Now we have something to work on. The program above produces the following error: Error: no identifier for declarator Pragma!"help, this does not work!!!!!!!!!" The error is fixed by adding a mixin: mixin Pragma!("help, this does not work!!!!!!!!!"); Despite another error it actually works: hello help, this does not work!!!!!!!!! <-- IT WORKED Error: t!() has no effect The second error is fixed by another mixin: mixin t!(); Perhaps the example is too simplistic. Still, let's stay with it for further issues. Ali
Jul 30 2013
On Tuesday, 30 July 2013 at 20:34:32 UTC, Ali Çehreli wrote:On 07/30/2013 12:09 PM, JS wrote:Or just assigning it to enum... both are not great solutions...I already stated why this is not a proper example, I'm notusing Pragmain run time code(for lack of a better term). module main; import std.stdio; template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } template t() { Pragma!("help, this does not work!!!!!!!!!"); } void main() { enum msg = "hello"; Pragma!msg; t!(); }Thank you. Now we have something to work on. The program above produces the following error: Error: no identifier for declarator Pragma!"help, this does not work!!!!!!!!!" The error is fixed by adding a mixin: mixin Pragma!("help, this does not work!!!!!!!!!");Despite another error it actually works: hello help, this does not work!!!!!!!!! <-- IT WORKED Error: t!() has no effect The second error is fixed by another mixin: mixin t!(); Perhaps the example is too simplistic. Still, let's stay with it for further issues. AliI think that such behavior should not be an error but possibly a warning, if at all. And if you cared to read what I initially posted you would realize I already explained the same thing. The example maybe more clear but I stated, maybe in some convoluted and jumped way, that Pragma doesn't work *in* templates.. "When I try to use void instead of string and do something like Pragma!(msg) I get an error that the template has no effect." and later "It's goal is to debug templates and essentially just wrapping pragma to supply the __FILE__ info automatically. e.g., instead of having to do pragma(msg, __FILE__~amsg); I want to do Pragma!(amsg); "
Jul 30 2013
BTW, when I use mixin in my code, the error goes away but no pragma msg is produced, so it is not a solution.
Jul 30 2013
On Monday, 29 July 2013 at 23:09:20 UTC, JS wrote:I have created a template Pragma that emulates pragma but better, the problem is that I have to assign it to something which is very redundant in my code: enum temp = Pragma!(msg) e.g., template Pragma(alias amsg) { string Pragma(string file = __FILE__) { pragma(msg, amsg); return ""; } } When I try to use void instead of string and do something like Pragma!(msg) I get an error that the template has no effect. It does have an effect but what it is complaining about is exactly what I want. I've tried all kinds of combinations(mixins work but I then can't ise __FILE__) and nothing works. Maybe someone has an idea.You could call your template in a static CTor: ---- import std.stdio; template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } static this() { Pragma!("foo")(); } void main() { writeln("Hello world!"); } ---- Maybe this helps?
Jul 30 2013
On Tuesday, 30 July 2013 at 07:12:11 UTC, Namespace wrote:On Monday, 29 July 2013 at 23:09:20 UTC, JS wrote:This is useless. It's goal is to debug templates and essentially just wrapping pragma to supply the __FILE__ info automatically. e.g., instead of having to do pragma(msg, __FILE__~amsg); I want to do Pragma!(amsg); where Pragma custom formats the error or info message with the location where the message was initiated. Sticking it in a ctor will only print a message from that location.I have created a template Pragma that emulates pragma but better, the problem is that I have to assign it to something which is very redundant in my code: enum temp = Pragma!(msg) e.g., template Pragma(alias amsg) { string Pragma(string file = __FILE__) { pragma(msg, amsg); return ""; } } When I try to use void instead of string and do something like Pragma!(msg) I get an error that the template has no effect. It does have an effect but what it is complaining about is exactly what I want. I've tried all kinds of combinations(mixins work but I then can't ise __FILE__) and nothing works. Maybe someone has an idea.You could call your template in a static CTor: ---- import std.stdio; template Pragma(alias amsg) { void Pragma(string file = __FILE__) { pragma(msg, amsg); } } static this() { Pragma!("foo")(); } void main() { writeln("Hello world!"); } ---- Maybe this helps?
Jul 30 2013