July 07, 2022
On Thursday, 7 July 2022 at 13:59:17 UTC, 12345swordy wrote:
> Side note: Does D really needs function overloading? You can achieve the same effect by using meta-programming.
>
> - Alex

Just wondering, how do you achieve it using meta-programming?

July 07, 2022

On Thursday, 7 July 2022 at 15:19:59 UTC, Alexandru Ermicioi wrote:

>

On Thursday, 7 July 2022 at 13:59:17 UTC, 12345swordy wrote:

>

Side note: Does D really needs function overloading? You can achieve the same effect by using meta-programming.

  • Alex

Just wondering, how do you achieve it using meta-programming?

For non-virtual functions, just use a template function:

auto f(T)(T v)
{
	static if (is(T : int))
		// insert code for f(int) overload
	else
		...
}

It doesn't work for virtual functions as they can't be templates.

July 07, 2022

On Thursday, 7 July 2022 at 15:51:08 UTC, Nick Treleaven wrote:

>

For non-virtual functions, just use a template function:

auto f(T)(T v)
{
	static if (is(T : int))
		// insert code for f(int) overload
	else
		...
}

This solution feels like a huge downgrade compared to existing functionality. Reminds me of php or javascript, plus you'd need to handle yourself error handling if arguments don't match.

July 07, 2022
On 7/6/22 20:30, Walter Bright wrote:
> On 7/5/2022 4:19 PM, Timon Gehr wrote:
>> I.e., in terms of type checking, everything has already been figured out and works:
> 
> That example just hurts my brain :-/

Well, I don't recommend people use that specific pattern, but it makes a lot of sense that it works in this way.

`x=>x` type checks as a `T delegate(T)` for any `T`.

`(x){}` does not type check as a `T delegate(T)` for any `T`.

The delegate literal is polysemous, and when trying to determine matches, it's instantiated for each potential match. Therefore, if a function accepts a `T delegate(T)`, the lambda can decide with `static if` if there is a match or not, arbitrarily depending on `T`.

(Note that all instantiations have to type check, as D does not have SFINAE.)
July 08, 2022

On Thursday, 7 July 2022 at 15:51:08 UTC, Nick Treleaven wrote:

>

On Thursday, 7 July 2022 at 15:19:59 UTC, Alexandru Ermicioi wrote:

>

On Thursday, 7 July 2022 at 13:59:17 UTC, 12345swordy wrote:

>

Side note: Does D really needs function overloading? You can achieve the same effect by using meta-programming.

  • Alex

Just wondering, how do you achieve it using meta-programming?

For non-virtual functions, just use a template function:

auto f(T)(T v)
{
	static if (is(T : int))
		// insert code for f(int) overload
	else
		...
}

It doesn't work for virtual functions as they can't be templates.

This can be solved by having a top type named "Any" to indicate that it can be any type for virtual functions.

-Alex

July 08, 2022

On Friday, 8 July 2022 at 13:35:12 UTC, 12345swordy wrote:

>

On Thursday, 7 July 2022 at 15:51:08 UTC, Nick Treleaven wrote:

>

For non-virtual functions, just use a template function:

auto f(T)(T v)
{
	static if (is(T : int))
		// insert code for f(int) overload
	else
		...
}

It doesn't work for virtual functions as they can't be templates.

This can be solved by having a top type named "Any" to indicate that it can be any type for virtual functions.

Actually, you can already do this with variadic functions and TypeInfo. Take a look at the third example:

https://dlang.org/spec/function.html#d_style_variadic_functions

July 08, 2022

On Friday, 8 July 2022 at 14:11:08 UTC, Paul Backus wrote:

>

On Friday, 8 July 2022 at 13:35:12 UTC, 12345swordy wrote:

>

On Thursday, 7 July 2022 at 15:51:08 UTC, Nick Treleaven wrote:

>

For non-virtual functions, just use a template function:

auto f(T)(T v)
{
	static if (is(T : int))
		// insert code for f(int) overload
	else
		...
}

It doesn't work for virtual functions as they can't be templates.

This can be solved by having a top type named "Any" to indicate that it can be any type for virtual functions.

Actually, you can already do this with variadic functions and TypeInfo. Take a look at the third example:

https://dlang.org/spec/function.html#d_style_variadic_functions

"TypeInfo" isn't a Top type though, nor should it be treated as one.

1 2 3 4 5 6
Next ›   Last »