May 27, 2012 Which template is recommended. | ||||
|---|---|---|---|---|
| ||||
Below there are two routines with the same name.
Apparently both can be present at the same time.
Which one of the two is the recommended code?
import std.stdio;
interface ITest
{
int first();
int second();
}
class Test: ITest
{
int first()
{
writeln("first");
return 1;
}
int second()
{
writeln("second");
return 2;
}
}
void routine(T:ITest) (T a)
{
a.second();
}
void routine(T) (T a) if (is(T:ITest))
{
a.first();
}
int main()
{
routine(new Test());
return 0;
}
-----------------------
The above example prints out first.
| ||||
May 27, 2012 Re: Which template is recommended. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to sclytrack | On 27-05-2012 11:43, sclytrack wrote: > > Below there are two routines with the same name. > Apparently both can be present at the same time. > > Which one of the two is the recommended code? > > > > import std.stdio; > > interface ITest > { > int first(); > int second(); > } > > class Test: ITest > { > int first() > { > writeln("first"); > return 1; > } > > int second() > { > writeln("second"); > return 2; > } > } > > void routine(T:ITest) (T a) > { > a.second(); > } I would use this. It is much clearer. > > void routine(T) (T a) if (is(T:ITest)) > { > a.first(); > } > > int main() > { > routine(new Test()); > return 0; > } > > > ----------------------- > The above example prints out first. -- Alex Rønne Petersen alex@lycus.org http://lycus.org | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply