September 04, 2016
On 9/4/16 12:28 AM, Walter Bright wrote:
> On 9/3/2016 1:43 PM, Andrei Alexandrescu wrote:
>> On 9/3/16 10:43 PM, Walter Bright wrote:
>>> On 9/3/2016 8:34 AM, Manu via Digitalmars-d wrote:
>>>> And if either module doesn't have an instance of func?
>>>
>>> static if (traits(compiles, ModuleOf!T.func))
>>>     alias func = ModuleOf!T.func;
>>
>> What's an elegant way to encapsulate this as a stutter-free one-liner?
>> -- Andrei
>
> using a template and mixing it in

"Show me the mo^H^Hcode."
September 04, 2016
On 03.09.2016 13:24, Walter Bright wrote:
>
> Something like:
>
> void foo(T,U)(T t, U u)
> {
>     alias func = ModuleOf!T.func;
>     alias func = ModuleOf!U.func;
>
>     func(t, u);
> }

Does not work. Local overloads are not supported.
September 03, 2016
On 9/3/2016 4:46 PM, Andrei Alexandrescu wrote:
> On 9/4/16 12:28 AM, Walter Bright wrote:
>> On 9/3/2016 1:43 PM, Andrei Alexandrescu wrote:
>>> On 9/3/16 10:43 PM, Walter Bright wrote:
>>>> On 9/3/2016 8:34 AM, Manu via Digitalmars-d wrote:
>>>>> And if either module doesn't have an instance of func?
>>>>
>>>> static if (traits(compiles, ModuleOf!T.func))
>>>>     alias func = ModuleOf!T.func;
>>>
>>> What's an elegant way to encapsulate this as a stutter-free one-liner?
>>> -- Andrei
>>
>> using a template and mixing it in
>
> "Show me the mo^H^Hcode."

template foo(T)
{
    static if (traits(compiles, ModuleOf!T.func))
        foo = "alias func = " ~ ModuleOf!T.func ~ ";";
    else
	foo = "";
}

mixin(foo!T);

September 03, 2016
On 9/3/2016 5:36 PM, Timon Gehr wrote:
> Does not work. Local overloads are not supported.

Yeah, you're right, should have tested that:

  void abc(int);
  void def(uint);

  void foo()
  {
    alias func = abc;
    alias func = def; // error

      func(1);
  }

fails. Pushing it out a level works:

  void abc(int);
  void def(uint);

  template foo()
  {
    alias func = abc;
    alias func = def;

    void foo()
    {
        func(1);
    }
  }

  void main()
  {
    foo();
  }
September 04, 2016
On Sunday, 4 September 2016 at 01:34:47 UTC, Walter Bright wrote:
> On 9/3/2016 5:36 PM, Timon Gehr wrote:
>> Does not work. Local overloads are not supported.
>
> Yeah, you're right, should have tested that:
>
>   void abc(int);
>   void def(uint);
>
>   void foo()
>   {
>     alias func = abc;
>     alias func = def; // error
>
>       func(1);
>   }
>
> fails. Pushing it out a level works:
>
>   void abc(int);
>   void def(uint);
>
>   template foo()
>   {
>     alias func = abc;
>     alias func = def;
>
>     void foo()
>     {
>         func(1);
>     }
>   }
>
>   void main()
>   {
>     foo();
>   }

What do you think about making overloading and UFCS work with local symbols? There are workarounds, but nothing pretty.
September 04, 2016
On 9/3/2016 10:57 PM, ZombineDev wrote:
> What do you think about making overloading and UFCS work with local symbols?

I'd rather not. Let's make what we have work. There's an unending demand for new features in the core language.


> There are workarounds, but nothing pretty.

I don't regard this as a workaround. It's how things were designed to work in D. The fact that it isn't the minimum number of characters doesn't make it a workaround.
September 04, 2016
On 9/4/16 2:36 AM, Timon Gehr wrote:
> On 03.09.2016 13:24, Walter Bright wrote:
>>
>> Something like:
>>
>> void foo(T,U)(T t, U u)
>> {
>>     alias func = ModuleOf!T.func;
>>     alias func = ModuleOf!U.func;
>>
>>     func(t, u);
>> }
>
> Does not work. Local overloads are not supported.

Might be a sensible enhancement. Removing artificial limitations is good programming language design. Turtles! -- Andrei
September 04, 2016
On 9/4/16 3:26 AM, Walter Bright wrote:
> On 9/3/2016 4:46 PM, Andrei Alexandrescu wrote:
>> On 9/4/16 12:28 AM, Walter Bright wrote:
>>> On 9/3/2016 1:43 PM, Andrei Alexandrescu wrote:
>>>> On 9/3/16 10:43 PM, Walter Bright wrote:
>>>>> On 9/3/2016 8:34 AM, Manu via Digitalmars-d wrote:
>>>>>> And if either module doesn't have an instance of func?
>>>>>
>>>>> static if (traits(compiles, ModuleOf!T.func))
>>>>>     alias func = ModuleOf!T.func;
>>>>
>>>> What's an elegant way to encapsulate this as a stutter-free one-liner?
>>>> -- Andrei
>>>
>>> using a template and mixing it in
>>
>> "Show me the mo^H^Hcode."
>
> template foo(T)
> {
>     static if (traits(compiles, ModuleOf!T.func))
>         foo = "alias func = " ~ ModuleOf!T.func ~ ";";
>     else
>     foo = "";
> }
>
> mixin(foo!T);

Actually "func" i.e. the function's name must be a paraneter. No? -- Andrei
September 04, 2016
On 9/4/2016 5:31 AM, Andrei Alexandrescu wrote:
> On 9/4/16 3:26 AM, Walter Bright wrote:
>> On 9/3/2016 4:46 PM, Andrei Alexandrescu wrote:
>>> On 9/4/16 12:28 AM, Walter Bright wrote:
>>>> On 9/3/2016 1:43 PM, Andrei Alexandrescu wrote:
>>>>> On 9/3/16 10:43 PM, Walter Bright wrote:
>>>>>> On 9/3/2016 8:34 AM, Manu via Digitalmars-d wrote:
>>>>>>> And if either module doesn't have an instance of func?
>>>>>>
>>>>>> static if (traits(compiles, ModuleOf!T.func))
>>>>>>     alias func = ModuleOf!T.func;
>>>>>
>>>>> What's an elegant way to encapsulate this as a stutter-free one-liner?
>>>>> -- Andrei
>>>>
>>>> using a template and mixing it in
>>>
>>> "Show me the mo^H^Hcode."
>>
>> template foo(T)
>> {
>>     static if (traits(compiles, ModuleOf!T.func))
>>         foo = "alias func = " ~ ModuleOf!T.func ~ ";";
>>     else
>>     foo = "";
>> }
>>
>> mixin(foo!T);
>
> Actually "func" i.e. the function's name must be a paraneter. No? -- Andrei

Probably. The point is, the tools to do this are available features of D.
September 04, 2016
On 9/4/2016 5:30 AM, Andrei Alexandrescu wrote:
> Might be a sensible enhancement. Removing artificial limitations is good
> programming language design. Turtles! -- Andrei

The design of executable function bodies is very much "declare before use", quite unlike at the declaration levels which is all "order is not relevant". Changing this will have consequences (such as our discussion of exactly when a declaration becomes valid), and I just feel that function code is just easier to understand if "declare before use" is the rule, because that is how we reason about how it is executed.

Besides, I showed a method of how the overloads could be done with the existing language.