| Thread overview | ||||||
|---|---|---|---|---|---|---|
|
October 14, 2008 Templated interface | ||||
|---|---|---|---|---|
| ||||
Is it possible to create templated interfaces in D? And if possible than can anyone provide some good example on templated interface. | ||||
October 14, 2008 Re: Templated interface | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DF | On Tue, 14 Oct 2008 23:41:57 +0200, DF <deefriend@yahoo.com> wrote: > Is it possible to create templated interfaces in D? And if possible than can anyone provide some good example on templated interface. interface foo(T) { T property(); T property(T value); } class bar : foo!(int) { int data; int property() { return data; } int property(int value) { return data = value; } } void main() { bar b = new bar; b.property = 4; writefln(b.property); } -- Simen | |||
October 14, 2008 Re: Templated interface | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | Simen Kjaeraas Wrote:
> On Tue, 14 Oct 2008 23:41:57 +0200, DF <deefriend@yahoo.com> wrote:
>
> > Is it possible to create templated interfaces in D? And if possible than can anyone provide some good example on templated interface.
>
> interface foo(T)
> {
> T property();
> T property(T value);
> }
>
> class bar : foo!(int)
> {
> int data;
>
> int property()
> {
> return data;
> }
>
> int property(int value)
> {
> return data = value;
> }
> }
>
> void main()
> {
> bar b = new bar;
>
> b.property = 4;
>
> writefln(b.property);
> }
>
> --
> Simen
Thanx, Simen! I've made the solution myself. Maybe if anyone is interested I can post it. Though your sampe is good, I've got more complicated thing:
interface Stack(T) {
void push(T element);
T pop();
int isEmpty();
}
class StackImpl(T) : Stack!(T) {
private T[] data;
this() {
}
public void push(T element) {
...
}
public T pop() {
...
}
public int isEmpty() {
...
}
}
void main() {
Stack!(char) data = new StackImpl!(char)();
}
Seems to work... :)
| |||
October 14, 2008 Re: Templated interface | ||||
|---|---|---|---|---|
| ||||
Posted in reply to DF | DF wrote:
> Is it possible to create templated interfaces in D? And if possible than can anyone provide some good example on templated interface.
Example:
IList(T)
A list of elements of type T. Implementations could be LinkedList(T) and ArrayList(T).
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply