Thread overview |
---|
July 29, 2013 use template function without assignment | ||||
---|---|---|---|---|
| ||||
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. |
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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);
}
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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.
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Tuesday, 30 July 2013 at 01:19:23 UTC, Meta wrote:
> 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.
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 work
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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?
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Tuesday, 30 July 2013 at 07:12:11 UTC, Namespace wrote:
> 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?
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.
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 work What 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. |
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | 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 |
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Tuesday, 30 July 2013 at 18:38:23 UTC, Ali Çehreli wrote:
> 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
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!();
}
|
July 30, 2013 Re: use template function without assignment | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | 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 |
Copyright © 1999-2021 by the D Language Foundation