September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic Attachments:
| On Mon, Sep 6, 2010 at 23:31, Andrej Mitrovic <andrej.mitrovich@test.com>wrote: > That still won't work. Observe: > template isInputRange(R) > { > enum bool isInputRange = isValidCode!( > { > R r; // can define a range object > if (r.empty) {} // can test for empty > r.popFront; // can invoke next > auto h = r.front; // can get the front of the range > }); > } > > template isValidCode(alias code) { enum bool isValidCode = > __traits(compiles, code); } > > Instead of returning false, it will give out a compiler error. > That's because the part between the curly braces is evaluated before being passed to the template. And there is no lazy alias. As Mafi said, you can use a string, it's still the best way to move code around in D. With q{ ... }, it's palatable. And no, before you try it, there is no way to pass the {...} to another template that would stringify it into a q{...} :-) Maybe, eventually, something like this: import std.stdio; template isValidCode(alias code) { template For(T) { enum bool For = __traits(compiles, code(T.init)); } } void main() { // use an anonymous templated function: alias isValidCode!((r) { if (r.empty) {} // can test for empty r.popFront; // can invoke next auto h = r.front; // c } ) isInputRange; // writeln(isInputRange.For!(int[])); } Except DMD doesn't like the commented-out line. Whaoh! Philippe |
September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> enum a = "test";
> a = "test2";
>
> Because it seems to compile. But that shouldn't work afaik..?
I have two open bug reports on this (and a third one was open by Don).
Bye,
bearophile
|
September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | I'm sorry, but what does q{..} mean?
Philippe Sigaud Wrote:
> On Mon, Sep 6, 2010 at 23:31, Andrej Mitrovic <andrej.mitrovich@test.com>wrote:
>
> > That still won't work. Observe:
>
>
>
> > template isInputRange(R)
> > {
> > enum bool isInputRange = isValidCode!(
> > {
> > R r; // can define a range object
> > if (r.empty) {} // can test for empty
> > r.popFront; // can invoke next
> > auto h = r.front; // can get the front of the range
> > });
> > }
> >
> > template isValidCode(alias code) { enum bool isValidCode =
> > __traits(compiles, code); }
> >
> > Instead of returning false, it will give out a compiler error.
> >
>
> That's because the part between the curly braces is evaluated before being
> passed to the template. And there is no lazy alias.
> As Mafi said, you can use a string, it's still the best way to move code
> around in D. With q{ ... }, it's palatable.
> And no, before you try it, there is no way to pass the {...} to another
> template that would stringify it into a q{...} :-)
>
> Maybe, eventually, something like this:
>
> import std.stdio;
>
>
> template isValidCode(alias code)
> {
> template For(T)
> {
> enum bool For = __traits(compiles, code(T.init));
> }
> }
>
> void main()
> {
> // use an anonymous templated function:
> alias isValidCode!((r)
> {
> if (r.empty) {} // can test for empty
> r.popFront; // can invoke next
> auto h = r.front; // c
> }
> ) isInputRange;
>
> // writeln(isInputRange.For!(int[]));
> }
>
> Except DMD doesn't like the commented-out line. Whaoh!
>
>
>
> Philippe
>
|
September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> I'm sorry, but what does q{..} mean?
q{} is just a different syntax to write "" or ``
It's a controversial feature. q{} isn't recognized by editors as a string, so they colour the syntax it contains normally as code, and not as a string. So it's a bit useful if you want to give a string to a higher order function like map, instead of a delegate, and you want to keep the visual illusion of a delegate:
map!q{a * a}([1, 2, 3])
The problem comes straight from its purpose: is that it doesn't look like a string, so its true nature is a bit hidden; and this may cause some troubles.
Another possible problem was discussed when the q{} syntax was introduced. It's not a clean syntax, it's a hack from the point of view of parsing/lexing too.
It's handy, but it may cause troubles too. I am getting used to it, but it's a untidy hack and it will keep being nothing more than a hack. And sometimes hacks later come back and bite your bum.
Bye,
bearophile
|
September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | Heh. I'd rather want text editors to use syntax highlighting on comments as well, but use a different background color. Then I would know it's a comment but it would also make any embedded code in the comment actually readable.
bearophile < bearophileHUGS@lycos.com> Wrote:
> Andrej Mitrovic:
>
> > I'm sorry, but what does q{..} mean?
>
> q{} is just a different syntax to write "" or ``
>
> It's a controversial feature. q{} isn't recognized by editors as a string, so they colour the syntax it contains normally as code, and not as a string. So it's a bit useful if you want to give a string to a higher order function like map, instead of a delegate, and you want to keep the visual illusion of a delegate:
>
> map!q{a * a}([1, 2, 3])
>
> The problem comes straight from its purpose: is that it doesn't look like a string, so its true nature is a bit hidden; and this may cause some troubles.
>
> Another possible problem was discussed when the q{} syntax was introduced. It's not a clean syntax, it's a hack from the point of view of parsing/lexing too.
>
> It's handy, but it may cause troubles too. I am getting used to it, but it's a untidy hack and it will keep being nothing more than a hack. And sometimes hacks later come back and bite your bum.
>
> Bye,
> bearophile
|
September 06, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | I meant string literals. But comments as well.
Andrej Mitrovic Wrote:
> Heh. I'd rather want text editors to use syntax highlighting on comments as well, but use a different background color. Then I would know it's a comment but it would also make any embedded code in the comment actually readable.
>
> bearophile < bearophileHUGS@lycos.com> Wrote:
>
> > Andrej Mitrovic:
> >
> > > I'm sorry, but what does q{..} mean?
> >
> > q{} is just a different syntax to write "" or ``
> >
> > It's a controversial feature. q{} isn't recognized by editors as a string, so they colour the syntax it contains normally as code, and not as a string. So it's a bit useful if you want to give a string to a higher order function like map, instead of a delegate, and you want to keep the visual illusion of a delegate:
> >
> > map!q{a * a}([1, 2, 3])
> >
> > The problem comes straight from its purpose: is that it doesn't look like a string, so its true nature is a bit hidden; and this may cause some troubles.
> >
> > Another possible problem was discussed when the q{} syntax was introduced. It's not a clean syntax, it's a hack from the point of view of parsing/lexing too.
> >
> > It's handy, but it may cause troubles too. I am getting used to it, but it's a untidy hack and it will keep being nothing more than a hack. And sometimes hacks later come back and bite your bum.
> >
> > Bye,
> > bearophile
>
|
September 07, 2010 Re: Understanding isInfinite(Range) | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 09/07/2010 12:44 AM, bearophile wrote:
> Andrej Mitrovic:
>
>> I'm sorry, but what does q{..} mean?
>
> q{} is just a different syntax to write "" or ``
>
> It's a controversial feature. q{} isn't recognized by editors as a string, so they colour the syntax it contains normally as code, and not as a string. So it's a bit useful if you want to give a string to a higher order function like map, instead of a delegate, and you want to keep the visual illusion of a delegate:
>
> map!q{a * a}([1, 2, 3])
>
> The problem comes straight from its purpose: is that it doesn't look like a string, so its true nature is a bit hidden; and this may cause some troubles.
>
> Another possible problem was discussed when the q{} syntax was introduced. It's not a clean syntax, it's a hack from the point of view of parsing/lexing too.
>
> It's handy, but it may cause troubles too. I am getting used to it, but it's a untidy hack and it will keep being nothing more than a hack. And sometimes hacks later come back and bite your bum.
>
> Bye,
> bearophile
It's not the same. Try q{\n}.
It's lexed like code.
|
Copyright © 1999-2021 by the D Language Foundation