Jump to page: 1 25  
Page
Thread overview
Template functions, can we make it more simple?
Aug 02, 2013
SteveGuo
Aug 02, 2013
monarch_dodra
Aug 02, 2013
SteveGuo
Aug 02, 2013
Tommi
Aug 02, 2013
SteveGuo
Aug 02, 2013
Timon Gehr
Aug 02, 2013
SteveGuo
Aug 02, 2013
monarch_dodra
Aug 02, 2013
SteveGuo
Aug 02, 2013
monarch_dodra
Aug 02, 2013
SteveGuo
Aug 02, 2013
SteveGuo
Aug 02, 2013
Dicebot
Aug 02, 2013
SteveGuo
Aug 02, 2013
Dicebot
Aug 02, 2013
SteveGuo
Aug 02, 2013
Andrej Mitrovic
Aug 02, 2013
Timon Gehr
Aug 02, 2013
Timon Gehr
Aug 02, 2013
H. S. Teoh
Aug 02, 2013
SteveGuo
Aug 02, 2013
H. S. Teoh
Aug 02, 2013
SteveGuo
Aug 02, 2013
H. S. Teoh
Aug 02, 2013
SteveGuo
Aug 10, 2013
H. S. Teoh
Aug 02, 2013
Dicebot
Aug 02, 2013
SteveGuo
Aug 02, 2013
QAston
Aug 02, 2013
Walter Bright
Aug 02, 2013
SteveGuo
Aug 03, 2013
F i L
Aug 03, 2013
John Colvin
Aug 03, 2013
Suliman
Aug 03, 2013
monarch_dodra
Aug 03, 2013
F i L
Aug 03, 2013
monarch_dodra
Aug 03, 2013
F i L
Aug 03, 2013
Flamaros
August 02, 2013
I'm not an expert on programming language, if I made a naive issue, just forgive me:p

Can we declare a template function like this?

auto Add(a, b) // Note a, b do not have type, that means a and b use template type
{
    return a + b;
}

auto Sub(a, int b) // a uses template type, b is fixed to int
{
    return a - b;
}

When we call the function,

Add(1, 2); // deduced to be Add(int, int);
Add(1.5, 2.3); // deduced to be Add(double, double);
Add(1.5, "Hello"); // compiler error!

Sub(1.5, 1); // deduced to be Add(double, int);
Sub(1, 1.1); // deduced to be Add(int, int); compiler error, double can not converted to int automatically

August 02, 2013
On Friday, 2 August 2013 at 20:34:04 UTC, SteveGuo wrote:
> I'm not an expert on programming language, if I made a naive issue, just forgive me:p

Yes, this would have been better asked in .learn, but no matter.

> Can we declare a template function like this?
>
> auto Add(a, b) // Note a, b do not have type, that means a and b use template type
> {
>     return a + b;
> }
>
> auto Sub(a, int b) // a uses template type, b is fixed to int
> {
>     return a - b;
> }
>
> When we call the function,
>
> Add(1, 2); // deduced to be Add(int, int);
> Add(1.5, 2.3); // deduced to be Add(double, double);
> Add(1.5, "Hello"); // compiler error!
>
> Sub(1.5, 1); // deduced to be Add(double, int);
> Sub(1, 1.1); // deduced to be Add(int, int); compiler error, double can not converted to int automatically

Types can be deduced automatically, so you can simply write:
auto Add(A, B)(A a, B b);

The type is explicit in the *declaration*, but when you call "add(1, 2)", the compiler will *deduce* A and B to be int.

Ditto for sub:
auto Sub(A a)(A a, int b);
August 02, 2013
On Friday, 2 August 2013 at 20:37:55 UTC, monarch_dodra wrote:
> On Friday, 2 August 2013 at 20:34:04 UTC, SteveGuo wrote:
>> I'm not an expert on programming language, if I made a naive issue, just forgive me:p
>
> Yes, this would have been better asked in .learn, but no matter.
>
>> Can we declare a template function like this?
>>
>> auto Add(a, b) // Note a, b do not have type, that means a and b use template type
>> {
>>    return a + b;
>> }
>>
>> auto Sub(a, int b) // a uses template type, b is fixed to int
>> {
>>    return a - b;
>> }
>>
>> When we call the function,
>>
>> Add(1, 2); // deduced to be Add(int, int);
>> Add(1.5, 2.3); // deduced to be Add(double, double);
>> Add(1.5, "Hello"); // compiler error!
>>
>> Sub(1.5, 1); // deduced to be Add(double, int);
>> Sub(1, 1.1); // deduced to be Add(int, int); compiler error, double can not converted to int automatically
>
> Types can be deduced automatically, so you can simply write:
> auto Add(A, B)(A a, B b);
>
> The type is explicit in the *declaration*, but when you call "add(1, 2)", the compiler will *deduce* A and B to be int.
>
> Ditto for sub:
> auto Sub(A a)(A a, int b);

Thanks for reply!

auto Add(A, B)(A a, B b); // Yes, this is the template function declaration of D manner

But I mean if the syntax can be more simple?

auto Add(A, B)(A a, B b); // type A, B appears twice

// Can we just make it more simple?
auto Add(a, b)
{
}
August 02, 2013
On Friday, 2 August 2013 at 20:50:00 UTC, SteveGuo wrote:
> auto Add(A, B)(A a, B b); // Yes, this is the template function declaration of D manner
>
> But I mean if the syntax can be more simple?
>
> auto Add(A, B)(A a, B b); // type A, B appears twice
>
> // Can we just make it more simple?
> auto Add(a, b)
> {
> }

I think it could get pretty confusing. I.e.:

module foo;

struct a { }
struct b { }

----
module bar;

import foo;

auto Add(a, b) { } // [1]

----
[1] Here the unsuspecting programmer thinks he's writing a template function, not knowing that one module he's importing actually specifies 'a' and 'b' as types, which makes his Add a regular function taking unnamed variables of types 'a' and 'b'.
August 02, 2013
On Friday, 2 August 2013 at 20:50:00 UTC, SteveGuo wrote:
> But I mean if the syntax can be more simple?
>
> auto Add(A, B)(A a, B b); // type A, B appears twice
>
> // Can we just make it more simple?
> auto Add(a, b)
> {
> }

Question is "should we"? It creates syntax ambiguity (a and b can be already existing types) and does not really improve language.
August 02, 2013
>
> I think it could get pretty confusing. I.e.:
>
> module foo;
>
> struct a { }
> struct b { }
>
> ----
> module bar;
>
> import foo;
>
> auto Add(a, b) { } // [1]
>
> ----
> [1] Here the unsuspecting programmer thinks he's writing a template function, not knowing that one module he's importing actually specifies 'a' and 'b' as types, which makes his Add a regular function taking unnamed variables of types 'a' and 'b'.

We know that normal function parameter declaration is the form of "PARAMTYPE param"
but in this declaration "auto Add(a, b) { }" the parameter do not have PARAMTYPE, compiler can detect this issue, so, the compiler knows a or b is not a type, it treats them as template
August 02, 2013
On 08/02/2013 11:20 PM, SteveGuo wrote:
>>
>> I think it could get pretty confusing. I.e.:
>>
>> module foo;
>>
>> struct a { }
>> struct b { }
>>
>> ----
>> module bar;
>>
>> import foo;
>>
>> auto Add(a, b) { } // [1]
>>
>> ----
>> [1] Here the unsuspecting programmer thinks he's writing a template
>> function, not knowing that one module he's importing actually
>> specifies 'a' and 'b' as types, which makes his Add a regular function
>> taking unnamed variables of types 'a' and 'b'.
>
> We know that normal function parameter declaration is the form of
> "PARAMTYPE param"
> but in this declaration "auto Add(a, b) { }" the parameter do not have
> PARAMTYPE, compiler can detect this issue, so, the compiler knows a or b
> is not a type, it treats them as template

Note that currently this is parsed as having _only_ PARAMTYPE.
August 02, 2013
>
> Question is "should we"? It creates syntax ambiguity (a and b can be already existing types) and does not really improve language.

Sorry for my pool English:p

I think compiler can detect the difference.
auto Add(a, b) {} // Compiler can detect "there is nothing in front of a and b, so a and b is template, not types defined in other modules"
August 02, 2013
On Friday, 2 August 2013 at 21:20:55 UTC, SteveGuo wrote:
> We know that normal function parameter declaration is the form of "PARAMTYPE param"

Wrong. Specifying only types was perfectly legal in C and remains so in D. It would have been a major breaking change.
August 02, 2013
> Note that currently this is parsed as having _only_ PARAMTYPE.

I just tested _only_

void foo(_only_ a)
{
}

but the compiler complaints "undefined identifier _only_"
« First   ‹ Prev
1 2 3 4 5