static interface

grauzone none at example.net
Mon Nov 16 12:00:01 PST 2009


Leandro Lucarella wrote:
> Why not? ;)

The actual question is if Andrei/Walter are interested at all in this. 
Because they didn't show any interest so far. D will probably be doomed 
to compile time bug-typing I mean duck-typing forever.

> static interface InputRange(T) {
> 	bool empty();
> 	T front();
> 	void popFront();
> }
> 
> size_t walkLength(InputRange range, size_t upTo = size_t.max)
> {
> 	// implementation
> }
> 
> struct Stride(T): InputRange(T) {
> 	// implementation
> }

Or just:

interface InputRange(T) {
	...
}

struct Stride(T) : InputRange(T) { ... }

size_t size_t walkLength(Range : InputRange)(Range range, ...

The main purpose of the interface is to allow the compiler to do some 
type checking.

(Interestingly, you could get an actual interface from a struct pointer. 
The compiler just needs to statically allocate a vtable (similar to a 
TypeInfo when writing typeid(T)). But I don't know if it'd be useful and 
is besides the point.)

Actually I'd prefer your proposal, because walkLength could access only 
members that are actually in InputRange. But the declaration looks too 
much like an untemplated function.

On the other hand, I remember that D2 once wanted to make this possible:

void foo(static int x, int y) { ... }

becomes

void foo(int x)(int y) { ... }

What happened to this? If it gets/got implemented, it would provide a 
nice opportunity to add syntax for such static interfaces.



More information about the Digitalmars-d mailing list