digitalmars.D.learn - Specify the type but not number of function arguments in an interface?
- Doctor J (5/5) Jul 03 2009 I want to write an interface that expresses the following idea: "classes...
- Jarrett Billingsley (25/30) Jul 03 2009 implementing this interface must have a void function named update, with...
- downs (3/4) Jul 05 2009 Use a typesafe variadic function, i.e.
- Jarrett Billingsley (2/6) Jul 05 2009 Read the rest of his post, downs. Particularly option 2.
- downs (2/11) Jul 07 2009 Oops.
- downs (2/13) Jul 07 2009 But then isn't what he asks for kinda impossible by design? I mean, inte...
- Jarrett Billingsley (2/3) Jul 07 2009 Now read *my* post, downs. ;)
- downs (2/7) Jul 08 2009 :blushes:
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
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
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
On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:Doctor J wrote:Read the rest of his post, downs. Particularly option 2.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
Jarrett Billingsley wrote:On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:Oops.Doctor J wrote:Read the rest of his post, downs. Particularly option 2.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 07 2009
downs wrote:Jarrett Billingsley 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_.On Sun, Jul 5, 2009 at 5:44 AM, downs<default_357-line yahoo.de> wrote:Oops.Doctor J wrote:Read the rest of his post, downs. Particularly option 2.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 07 2009
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
Jarrett Billingsley wrote:On Tue, Jul 7, 2009 at 8:29 AM, downs<default_357-line yahoo.de> wrote::blushes: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 08 2009