www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: static interface

reply Jesse Phillips <jessekphillips+D gamil.com> writes:
Leandro Lucarella Wrote:

 
 The problem is "regular" interfaces provide dynamic dispatch. Andrei could
 implement all the range stuff using interfaces, but that would mean:
 1) You have to inherit from the interface (i.e., you can't use arrays)
 2) All calls to ranges functions are virtual (inefficient; this is
    particularly relevant since they are called inside loops => lot of
    times)
 
 A static interface don't have those problems, and I don't see a way to mix
 static and dynamic interfaces without introducing a "new" type of
 interfaces (static interface).

I realize that a class that inherits an interface would have these issues, but if you aren't required to inherit the interface and the use of interface in a function parameter constitutes a compile-time check instead of inheritance check... But this does bring up the question in your suggestion the following code couldn't compile, but based on the signature it looks like it should: size_t walkLength(InputRange range, size_t upTo = size_t.max) { InputRange temp = range; // implementation } I only know enough about the inner workings of compiling Classes to be able to follow explanation why my ideas won't work. This could give a very clear compiler error, but just doesn't appear wrong outside of that.
Nov 16 2009
parent Leandro Lucarella <llucax gmail.com> writes:
Jesse Phillips, el 16 de noviembre a las 19:14 me escribiste:
 Leandro Lucarella Wrote:
 
 
 The problem is "regular" interfaces provide dynamic dispatch. Andrei could
 implement all the range stuff using interfaces, but that would mean:
 1) You have to inherit from the interface (i.e., you can't use arrays)
 2) All calls to ranges functions are virtual (inefficient; this is
    particularly relevant since they are called inside loops => lot of
    times)
 
 A static interface don't have those problems, and I don't see a way to mix
 static and dynamic interfaces without introducing a "new" type of
 interfaces (static interface).

I realize that a class that inherits an interface would have these issues, but if you aren't required to inherit the interface and the use of interface in a function parameter constitutes a compile-time check instead of inheritance check... But this does bring up the question in your suggestion the following code couldn't compile, but based on the signature it looks like it should: size_t walkLength(InputRange range, size_t upTo = size_t.max) { InputRange temp = range; // implementation }

That can be translated to something like: size_t walkLength(Range)(Range range, size_t upTo = size_t.max) if (isInputRange!(Range)) { Range temp = range; // implementation } -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- DETUVIERON BANDA DE PIRATAS DEL ASFALTO SON TODOS URUGUAYOS Y ROBARON MILES DE LITROS DE CERVEZA -- Crónica TV
Nov 17 2009