www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Specify the type but not number of function arguments in an interface?

reply Doctor J <nobody nowhere.com> writes:
I want to write an interface that expresses the following idea: "classes
implementing this interface must have a void function named update, with a
fixed but indeterminate number of parameters of the same (template parameter)
type."  In other words, some implementing classes will have void update(T a),
some void update(T a, T b), etc.  Admittedly, this kind of defeats the purpose
of an interface, but I thought I would try to hack around it anyhow.  :)

Option 1: put lots of update() functions in the interface, one with each
possible number of parameters; implement every one in every derived class; and
spew some static asserts if you try to use the wrong ones.  Yuck.

Option 2: variadic functions: declare void update(T[] params ...) in the
interface, and try to specialize to a fixed number of parameters in
implementing classes.  D doesn't want to let me do this literally, and
params.length nor _arguments.length are static constants -- though it seems
like they could be.  I want to catch calls with the wrong number of arguments
at compile time.

Option 3: variadic templates: declare void update(P...)(P args) in the
interface, and then define what you like in implementing classes.  This works,
but too well: there doesn't seem to be any way to constrain the type of the
arguments, which I would like the interface to do.

Other options?  Is there a way to do this?
Jul 03 2009
next sibling parent Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Fri, Jul 3, 2009 at 2:54 PM, Doctor J<nobody nowhere.com> wrote:
 I want to write an interface that expresses the following idea: "classes =
implementing this interface must have a void function named update, with a = fixed but indeterminate number of parameters of the same (template paramete= r) type." =A0In other words, some implementing classes will have void updat= e(T a), some void update(T a, T b), etc. =A0Admittedly, this kind of defeat= s the purpose of an interface, but I thought I would try to hack around it = anyhow. =A0:)
 Option 1: put lots of update() functions in the interface, one with each =
possible number of parameters; implement every one in every derived class; = and spew some static asserts if you try to use the wrong ones. =A0Yuck.
 Option 2: variadic functions: declare void update(T[] params ...) in the =
interface, and try to specialize to a fixed number of parameters in impleme= nting classes. =A0D doesn't want to let me do this literally, and params.le= ngth nor _arguments.length are static constants -- though it seems like the= y could be. =A0I want to catch calls with the wrong number of arguments at = compile time.
 Option 3: variadic templates: declare void update(P...)(P args) in the in=
terface, and then define what you like in implementing classes. =A0This wor= ks, but too well: there doesn't seem to be any way to constrain the type of= the arguments, which I would like the interface to do.
 Other options? =A0Is there a way to do this?
No, not really. The entire purpose of putting a function in an interface is so that anything that implements it conforms to it exactly, so you can call the method on any interface reference. How the heck would you call .update() on an interface reference if you didn't know, at compile time, how many params the derived class's update method took? This sounds more like a job for duck typing, but I don't know what you're d= oing.
Jul 03 2009
prev sibling parent reply downs <default_357-line yahoo.de> writes:
Doctor J wrote:
 I want to write an interface that expresses the following idea: "classes
implementing this interface must have a void function named update, with a
fixed but indeterminate number of parameters of the same (template parameter)
type." 
Use a typesafe variadic function, i.e. void test(int[] integers...);
Jul 05 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:
 Doctor J wrote:
 I want to write an interface that expresses the following idea: "classes
implementing this interface must have a void function named update, with a
fixed but indeterminate number of parameters of the same (template parameter)
type."
Use a typesafe variadic function, i.e. void test(int[] integers...);
Read the rest of his post, downs. Particularly option 2.
Jul 05 2009
parent reply downs <default_357-line yahoo.de> writes:
Jarrett Billingsley wrote:
 On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:
 Doctor J wrote:
 I want to write an interface that expresses the following idea: "classes
implementing this interface must have a void function named update, with a
fixed but indeterminate number of parameters of the same (template parameter)
type."
Use a typesafe variadic function, i.e. void test(int[] integers...);
Read the rest of his post, downs. Particularly option 2.
Oops.
Jul 07 2009
parent reply downs <default_357-line yahoo.de> writes:
downs wrote:
 Jarrett Billingsley wrote:
 On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:
 Doctor J wrote:
 I want to write an interface that expresses the following idea: "classes
implementing this interface must have a void function named update, with a
fixed but indeterminate number of parameters of the same (template parameter)
type."
Use a typesafe variadic function, i.e. void test(int[] integers...);
Read the rest of his post, downs. Particularly option 2.
Oops.
But then isn't what he asks for kinda impossible by design? I mean, interfaces are bound at runtime. That's what they _do_.
Jul 07 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Jul 7, 2009 at 8:29 AM, downs<default_357-line yahoo.de> wrote:
 But then isn't what he asks for kinda impossible by design? I mean, interfaces
are bound at runtime. That's what they _do_.
Now read *my* post, downs. ;)
Jul 07 2009
parent downs <default_357-line yahoo.de> writes:
Jarrett Billingsley wrote:
 On Tue, Jul 7, 2009 at 8:29 AM, downs<default_357-line yahoo.de> wrote:
 But then isn't what he asks for kinda impossible by design? I mean, interfaces
are bound at runtime. That's what they _do_.
Now read *my* post, downs. ;)
:blushes:
Jul 08 2009