February 27, 2016
On Friday, 26 February 2016 at 23:11:32 UTC, Andrei Alexandrescu wrote:
> On 02/26/2016 06:09 PM, Andrei Alexandrescu wrote:
>> static if (is(partition == function) || is(partition == delegate))
>>    partition(r);
>> else if (__traits(compiles, partition!less(r, n)))
>>    partition!less(r, n);
>> else
>>    partition!less(r);
>>
> Nevertheless, I'm still on lookout for a more elegant solution! I have this mindset that using __traits(compiles) is some sort of cheating.

I don't see it as cheating; in fact it is more elegant, because it checks for exactly the thing you depend on, namely that `partition` is callable. Your explicit check above rejects structs with opCall(), for example, which is probably not what you intended.
February 27, 2016
On 02/27/2016 07:38 AM, Marc Schütz wrote:
> I don't see it as cheating; in fact it is more elegant, because it
> checks for exactly the thing you depend on, namely that `partition` is
> callable. Your explicit check above rejects structs with opCall(), for
> example, which is probably not what you intended.

Good point. Unrelated: I also noticed that if the template has an error inside, the error messages when using it with __traits(compiles) are mightily confusing. -- Andrei
February 27, 2016
On 27.02.2016 01:03, Jonathan M Davis wrote:
>
> There's nothing cheating about using __traits(compiles). It's basically
> the same as using is(typeof(foo)) and is(typeof({statement;})), albeit
> arguably a bit more explicit about the fact that it's testing what compiles.

The two are subtly different and only __traits(compiles,...) reliably checks for compilability. Never use is(typeof(...)) unless you know exactly what you are doing.

void main(){
    int x;
    static void foo(){
        static assert(is(typeof({return x;})));
        static assert(!__traits(compiles,{return x;}));
        //auto a={return x;}; // error
    }
}

February 29, 2016
On 02/27/2016 01:09 AM, Andrei Alexandrescu wrote:
> static if (is(partition == function) || is(partition == delegate))
>   partition(r);
> else if (__traits(compiles, partition!less(r, n)))
>   partition!less(r, n);
> else
>   partition!less(r);
> 
> The first test works very nice. The second does not; the compiler attempts to instantiate the template wrongly and spits a bunch of errors before giving up, in spite of the whole "let me know silently whether this compiles" thing.
> 
> So, what's an elegant solution to this? I looked up std.traits but nothing seems to help. (Tried arity, no avail.)

Untested:

bool hasArityOverload ( alias F ) ( )
{
    import std.traits;
    if (!isSomeFunction!(F!less))
        return false;
    alias ParamTypes = Parameters!(F!less);
    if (ParamTypes.length != 2)
        return false;
    return isInputRange!(ParamTypes[0]) && is(ParamTypes[0] : uint);
}

Much more verbose but kess chance of accidental passing/failing by unrelated reasons. In "casual" application code I'd still probably go with `__traits(compiles)`.
March 03, 2016
On Friday, 26 February 2016 at 23:09:52 UTC, Andrei Alexandrescu wrote:
> So, what's an elegant solution to this? I looked up std.traits but nothing seems to help. (Tried arity, no avail.)

There's this[1] PR which implements exactly what you want, but it's currently not passing the auto-tester.

1. https://github.com/D-Programming-Language/dmd/pull/5201


March 03, 2016
On 03/03/2016 02:54 PM, Meta wrote:
> On Friday, 26 February 2016 at 23:09:52 UTC, Andrei Alexandrescu wrote:
>> So, what's an elegant solution to this? I looked up std.traits but
>> nothing seems to help. (Tried arity, no avail.)
>
> There's this[1] PR which implements exactly what you want, but it's
> currently not passing the auto-tester.
>
> 1. https://github.com/D-Programming-Language/dmd/pull/5201

Thanks! That will push introspection considerably further. I toggled auto-pull. -- Andrei
March 03, 2016
On Thursday, 3 March 2016 at 21:06:12 UTC, Andrei Alexandrescu wrote:
> Thanks! That will push introspection considerably further. I toggled auto-pull. -- Andrei

I would recommend cancelling that as it's currently failing on Linux and FreeBSD.
March 04, 2016
On Thursday, 3 March 2016 at 21:06:12 UTC, Andrei Alexandrescu wrote:
> Thanks! That will push introspection considerably further. I toggled auto-pull. -- Andrei

I made the simplest change possible to get it to compile, PR here[1]. It adds an extra allocation so any suggestions on eliminating that are welcome.

1. https://github.com/D-Programming-Language/dmd/pull/5496
1 2
Next ›   Last »