April 28, 2015
On Tuesday, 28 April 2015 at 10:24:27 UTC, Andrea Fontana wrote:
> On Tuesday, 28 April 2015 at 10:18:49 UTC, John Colvin wrote:
>> On Tuesday, 28 April 2015 at 10:18:12 UTC, John Colvin wrote:
>>> On Tuesday, 28 April 2015 at 10:07:43 UTC, Andrea Fontana wrote:
>>>> On Tuesday, 28 April 2015 at 09:23:53 UTC, Chris wrote:
>>>>> And this has happened to me many times. The solution "Break the UFCS chain and use a local temporary variable" makes me angry, because by having to do so all the beauty of chaining is lost.
>>>>
>>>> A very slow (i guess) workaround could be:
>>>>
>>>> "test".toUpper.only.map!(a => "This is a " ~ a).front.writeln;
>>>>
>>>> vs the new one:
>>>>
>>>> "test".toUpper.Identity!(a => "This is a " ~ a).writeln;
>>>
>>> Shouldn't be slow, your just giving the optimiser some work to do*, but you're always better off with the second one.
>>>
>>> *Assuming a good optimiser. dmd won't work this out.
>>
>> s/your/you're
>
> Trying on d.godbolt.com it seems a lot of extra-code is generated for the first version.
>
> Anyway I think I'm going to rename it "apply". :)
>
> "test".toUpper.apply!(a => "This is a " ~ a).writeln;
>
> It sounds better.

The d.godbolt.org compilers seem a little out of date. I have found problems with their codegen/optimisation that doesn't happen in more recent versions.

ldc 0.15.2 based on llvm 3.5.1 reduces both chains to identical asm.
April 28, 2015
On 4/27/15 7:36 PM, Vladimir Panteleev wrote:
> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/

Went to post on reddit, was there already:

http://www.reddit.com/r/programming/comments/345zw3/the_amazing_template_that_does_nothing/

I've also posted on these other sites:

https://news.ycombinator.com/newest

https://twitter.com/D_Programming/status/593135475537223680

https://www.facebook.com/dlang.org/posts/1059305784083102


Andrei

April 28, 2015
On Tuesday, 28 April 2015 at 10:24:27 UTC, Andrea Fontana wrote:
> Trying on d.godbolt.com it seems a lot of extra-code is generated for the first version.

d.godbolt.com is dead, use asm.dlang.org
April 28, 2015
On Tuesday, 28 April 2015 at 19:32:40 UTC, Andrei Alexandrescu wrote:
> On 4/27/15 7:36 PM, Vladimir Panteleev wrote:
>> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/
>
> Went to post on reddit, was there already:
>
> http://www.reddit.com/r/programming/comments/345zw3/the_amazing_template_that_does_nothing/
>
> I've also posted on these other sites:

Thank you, though the article is kinda targeted at experienced D developers. As the redditors have been quick to point out, most of these are basically workarounds for grammar quirks.
April 28, 2015
On 4/27/15 10:36 PM, Vladimir Panteleev wrote:
> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/
>

Very cool.

Just a grammar nit, "an UFCS" should be "a UFCS".

-Steve
April 28, 2015
On Tuesday, 28 April 2015 at 21:42:04 UTC, Steven Schveighoffer wrote:
> On 4/27/15 10:36 PM, Vladimir Panteleev wrote:
>> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/
>>
>
> Very cool.
>
> Just a grammar nit, "an UFCS" should be "a UFCS".

Fixed, thanks. (I always found this rule counter-intuitive... "u" is a vowel dangit!)
April 28, 2015
On Tuesday, 28 April 2015 at 02:36:38 UTC, Vladimir Panteleev wrote:
> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/

A truly polymorphic identity function in D would be more involved:

template id(a...) if (a.length == 1)
{
    static if (__traits(compiles, { alias id = a[0]; } ))
        alias id = a[0];
    else
        enum id = a[0];
}

static assert(is(id!int == int));
static assert(id!1 == 1);

Also, an 'isEqual' template would be needed to unify 'isSame', 'is' and '=='.
April 28, 2015
On 4/28/15 6:00 PM, Vladimir Panteleev wrote:
> On Tuesday, 28 April 2015 at 21:42:04 UTC, Steven Schveighoffer wrote:
>> On 4/27/15 10:36 PM, Vladimir Panteleev wrote:
>>> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/
>>>
>>>
>>
>> Very cool.
>>
>> Just a grammar nit, "an UFCS" should be "a UFCS".
>
> Fixed, thanks. (I always found this rule counter-intuitive... "u" is a
> vowel dangit!)

And in most cases, 'an' is correct. It's only when it makes a "you" sound (and if you spell out your acronyms, 'U' does), when you want to use 'a' :)

an upsetting rule ('uh')
an uber-cool language ('oo')
a unique grammar problem ('you')

-Steve
April 28, 2015
On Tuesday, 28 April 2015 at 22:24:53 UTC, Max Samukha wrote:
> On Tuesday, 28 April 2015 at 02:36:38 UTC, Vladimir Panteleev wrote:
>> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/
>
> A truly polymorphic identity function in D would be more involved:
>
> template id(a...) if (a.length == 1)
> {
>     static if (__traits(compiles, { alias id = a[0]; } ))
>         alias id = a[0];
>     else
>         enum id = a[0];
> }
>
> static assert(is(id!int == int));
> static assert(id!1 == 1);
>
> Also, an 'isEqual' template would be needed to unify 'isSame', 'is' and '=='.

Actually I do find more specialized names/implementations useful in this case. For example, just the fact that you use `unaryFun!(a => a)` instead of `Identity!(a => a)` makes code more readable and intention clear even if effect is the same.
April 29, 2015
On Tuesday, 28 April 2015 at 02:36:38 UTC, Vladimir Panteleev wrote:
> http://blog.thecybershadow.net/2015/04/28/the-amazing-template-which-does-nothing/

Thanks for good article

little mistake: return from void function:

/// Search a website for something, and parse the
/// first search result's webpage.
void getItemInfo(string itemName)
{
    // Let's go! First, construct the URL.
    return ("http://www.example.com/search?q=" ~ encodeComponent(itemName))
     .......