Interface definition for static methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Livingstone

    #1

    Interface definition for static methods

    Anyone able to explain to me why you cannot define an interface that can
    then be implemented using static methods?

    I understand the C# CLS states this, but just interested in the reasons
    behind it.

    thanks,
    Steven


  • John Wood

    #2
    Re: Interface definition for static methods

    You mean *implement* the methods of an interface with a static modifier?
    Obviously the signature has to match. Or perhaps you mean having statics as
    part of the interface definition?

    Either way, interfaces are about defining a contract of method names that an
    *instance* has to support, statics aren't instance level methods they're
    class level.

    "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in message
    news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Anyone able to explain to me why you cannot define an interface that can
    > then be implemented using static methods?
    >
    > I understand the C# CLS states this, but just interested in the reasons
    > behind it.
    >
    > thanks,
    > Steven
    >
    >[/color]


    Comment

    • Steven Livingstone

      #3
      Re: Interface definition for static methods

      Right. Static methods can't be part of the interface definition though. Any
      reason?

      If i write a data layer (as i am doing...) it would be nice to define an
      interface that my class can then implement using static methods, rather than
      instance methods.

      "John Wood" <j@ro.com> wrote in message
      news:uUVSPp1XDH A.3248@tk2msftn gp13.phx.gbl...[color=blue]
      > You mean *implement* the methods of an interface with a static modifier?
      > Obviously the signature has to match. Or perhaps you mean having statics[/color]
      as[color=blue]
      > part of the interface definition?
      >
      > Either way, interfaces are about defining a contract of method names that[/color]
      an[color=blue]
      > *instance* has to support, statics aren't instance level methods they're
      > class level.
      >
      > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
      message[color=blue]
      > news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > Anyone able to explain to me why you cannot define an interface that can
      > > then be implemented using static methods?
      > >
      > > I understand the C# CLS states this, but just interested in the reasons
      > > behind it.
      > >
      > > thanks,
      > > Steven
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • John Wood

        #4
        Re: Interface definition for static methods

        So even if you could put static members in an interface and implement, how
        would that be useful?

        Are you going to be passing around the Type of the class, and somehow you'd
        expect to be able to cast that type to an interface?

        Seems a bit odd.

        "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in message
        news:u6Rhgu1XDH A.1204@TK2MSFTN GP12.phx.gbl...[color=blue]
        > Right. Static methods can't be part of the interface definition though.[/color]
        Any[color=blue]
        > reason?
        >
        > If i write a data layer (as i am doing...) it would be nice to define an
        > interface that my class can then implement using static methods, rather[/color]
        than[color=blue]
        > instance methods.
        >
        > "John Wood" <j@ro.com> wrote in message
        > news:uUVSPp1XDH A.3248@tk2msftn gp13.phx.gbl...[color=green]
        > > You mean *implement* the methods of an interface with a static modifier?
        > > Obviously the signature has to match. Or perhaps you mean having statics[/color]
        > as[color=green]
        > > part of the interface definition?
        > >
        > > Either way, interfaces are about defining a contract of method names[/color][/color]
        that[color=blue]
        > an[color=green]
        > > *instance* has to support, statics aren't instance level methods they're
        > > class level.
        > >
        > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
        > message[color=green]
        > > news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
        > > > Anyone able to explain to me why you cannot define an interface that[/color][/color][/color]
        can[color=blue][color=green][color=darkred]
        > > > then be implemented using static methods?
        > > >
        > > > I understand the C# CLS states this, but just interested in the[/color][/color][/color]
        reasons[color=blue][color=green][color=darkred]
        > > > behind it.
        > > >
        > > > thanks,
        > > > Steven
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Steven Livingstone

          #5
          Re: Interface definition for static methods

          Well, for a start it would make sure that the variosu data providers i have
          to write all conform to a specific interface. Is that odd? I would have
          though that would be pretty normal. The Data Application Blocks from MS
          implement their entire data layer using static methods. I'm sure if i wanted
          to write for a provider other than SqlServer I would like to just tell the
          implementer to implement according to *this* interface. Would help a lot at
          design time.

          As for the runtime behaviour, don't expect to be able to cast anything, more
          that i could do something like IData.MyStaticM ethod(). I don't know that
          this is so odd either. Many data layer class are implemented statically and
          it would be nice to able to have some reliable interface to make sure the
          static method i want to call exists!

          "John Wood" <jwood8@go_ahea d_remove_this.o ptonline.net> wrote in message
          news:bsuZa.2771 7$_R5.7863324@n ews4.srv.hcvlny .cv.net...[color=blue]
          > So even if you could put static members in an interface and implement, how
          > would that be useful?
          >
          > Are you going to be passing around the Type of the class, and somehow[/color]
          you'd[color=blue]
          > expect to be able to cast that type to an interface?
          >
          > Seems a bit odd.
          >
          > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
          message[color=blue]
          > news:u6Rhgu1XDH A.1204@TK2MSFTN GP12.phx.gbl...[color=green]
          > > Right. Static methods can't be part of the interface definition though.[/color]
          > Any[color=green]
          > > reason?
          > >
          > > If i write a data layer (as i am doing...) it would be nice to define an
          > > interface that my class can then implement using static methods, rather[/color]
          > than[color=green]
          > > instance methods.
          > >
          > > "John Wood" <j@ro.com> wrote in message
          > > news:uUVSPp1XDH A.3248@tk2msftn gp13.phx.gbl...[color=darkred]
          > > > You mean *implement* the methods of an interface with a static[/color][/color][/color]
          modifier?[color=blue][color=green][color=darkred]
          > > > Obviously the signature has to match. Or perhaps you mean having[/color][/color][/color]
          statics[color=blue][color=green]
          > > as[color=darkred]
          > > > part of the interface definition?
          > > >
          > > > Either way, interfaces are about defining a contract of method names[/color][/color]
          > that[color=green]
          > > an[color=darkred]
          > > > *instance* has to support, statics aren't instance level methods[/color][/color][/color]
          they're[color=blue][color=green][color=darkred]
          > > > class level.
          > > >
          > > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
          > > message[color=darkred]
          > > > news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..
          > > > > Anyone able to explain to me why you cannot define an interface that[/color][/color]
          > can[color=green][color=darkred]
          > > > > then be implemented using static methods?
          > > > >
          > > > > I understand the C# CLS states this, but just interested in the[/color][/color]
          > reasons[color=green][color=darkred]
          > > > > behind it.
          > > > >
          > > > > thanks,
          > > > > Steven
          > > > >
          > > > >
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • John Wood

            #6
            Re: Interface definition for static methods

            > As for the runtime behaviour, don't expect to be able to cast anything,
            more[color=blue]
            > that i could do something like IData.MyStaticM ethod(). I don't know that[/color]

            But when you do IData.MyStaticM ethod(), which class would it be referring
            to??

            I can understand that you may want to provide a good way to ensure that a
            class provides a specific set of static members, but other than that I can't
            see the use. You could possibly implement that type of validation using a
            custom attribute.

            "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in message
            news:%23jvHl41X DHA.1580@tk2msf tngp13.phx.gbl. ..[color=blue]
            > Well, for a start it would make sure that the variosu data providers i[/color]
            have[color=blue]
            > to write all conform to a specific interface. Is that odd? I would have
            > though that would be pretty normal. The Data Application Blocks from MS
            > implement their entire data layer using static methods. I'm sure if i[/color]
            wanted[color=blue]
            > to write for a provider other than SqlServer I would like to just tell the
            > implementer to implement according to *this* interface. Would help a lot[/color]
            at[color=blue]
            > design time.
            >
            > As for the runtime behaviour, don't expect to be able to cast anything,[/color]
            more[color=blue]
            > that i could do something like IData.MyStaticM ethod(). I don't know that
            > this is so odd either. Many data layer class are implemented statically[/color]
            and[color=blue]
            > it would be nice to able to have some reliable interface to make sure the
            > static method i want to call exists!
            >
            > "John Wood" <jwood8@go_ahea d_remove_this.o ptonline.net> wrote in message
            > news:bsuZa.2771 7$_R5.7863324@n ews4.srv.hcvlny .cv.net...[color=green]
            > > So even if you could put static members in an interface and implement,[/color][/color]
            how[color=blue][color=green]
            > > would that be useful?
            > >
            > > Are you going to be passing around the Type of the class, and somehow[/color]
            > you'd[color=green]
            > > expect to be able to cast that type to an interface?
            > >
            > > Seems a bit odd.
            > >
            > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
            > message[color=green]
            > > news:u6Rhgu1XDH A.1204@TK2MSFTN GP12.phx.gbl...[color=darkred]
            > > > Right. Static methods can't be part of the interface definition[/color][/color][/color]
            though.[color=blue][color=green]
            > > Any[color=darkred]
            > > > reason?
            > > >
            > > > If i write a data layer (as i am doing...) it would be nice to define[/color][/color][/color]
            an[color=blue][color=green][color=darkred]
            > > > interface that my class can then implement using static methods,[/color][/color][/color]
            rather[color=blue][color=green]
            > > than[color=darkred]
            > > > instance methods.
            > > >
            > > > "John Wood" <j@ro.com> wrote in message
            > > > news:uUVSPp1XDH A.3248@tk2msftn gp13.phx.gbl...
            > > > > You mean *implement* the methods of an interface with a static[/color][/color]
            > modifier?[color=green][color=darkred]
            > > > > Obviously the signature has to match. Or perhaps you mean having[/color][/color]
            > statics[color=green][color=darkred]
            > > > as
            > > > > part of the interface definition?
            > > > >
            > > > > Either way, interfaces are about defining a contract of method names[/color]
            > > that[color=darkred]
            > > > an
            > > > > *instance* has to support, statics aren't instance level methods[/color][/color]
            > they're[color=green][color=darkred]
            > > > > class level.
            > > > >
            > > > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in
            > > > message
            > > > > news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..
            > > > > > Anyone able to explain to me why you cannot define an interface[/color][/color][/color]
            that[color=blue][color=green]
            > > can[color=darkred]
            > > > > > then be implemented using static methods?
            > > > > >
            > > > > > I understand the C# CLS states this, but just interested in the[/color]
            > > reasons[color=darkred]
            > > > > > behind it.
            > > > > >
            > > > > > thanks,
            > > > > > Steven
            > > > > >
            > > > > >
            > > > >
            > > > >
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Steven Livingstone

              #7
              Re: Interface definition for static methods

              > But when you do IData.MyStaticM ethod(), which class would it be referring[color=blue]
              > to??[/color]

              Hey, it's a Sunday, i'm not on 100% today :) Imagining it were possible....
              I may use a helper class to return a pointer to the class containing the
              static/shared methods (much as i do with instance methods for just now),
              except i cache instance classes for the current user (which i wouldn't for
              statics obviously). So then you have your class which implements your
              interface and you can call the static methods on it as always.

              There is some interesting discussion on these subjects here



              "John Wood" <jwood8@go_ahea d_remove_this.o ptonline.net> wrote in message
              news:hQuZa.2786 2$_R5.7960479@n ews4.srv.hcvlny .cv.net...[color=blue][color=green]
              > > As for the runtime behaviour, don't expect to be able to cast anything,[/color]
              > more[color=green]
              > > that i could do something like IData.MyStaticM ethod(). I don't know that[/color]
              >
              > But when you do IData.MyStaticM ethod(), which class would it be referring
              > to??
              >
              > I can understand that you may want to provide a good way to ensure that a
              > class provides a specific set of static members, but other than that I[/color]
              can't[color=blue]
              > see the use. You could possibly implement that type of validation using a
              > custom attribute.
              >
              > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
              message[color=blue]
              > news:%23jvHl41X DHA.1580@tk2msf tngp13.phx.gbl. ..[color=green]
              > > Well, for a start it would make sure that the variosu data providers i[/color]
              > have[color=green]
              > > to write all conform to a specific interface. Is that odd? I would have
              > > though that would be pretty normal. The Data Application Blocks from MS
              > > implement their entire data layer using static methods. I'm sure if i[/color]
              > wanted[color=green]
              > > to write for a provider other than SqlServer I would like to just tell[/color][/color]
              the[color=blue][color=green]
              > > implementer to implement according to *this* interface. Would help a lot[/color]
              > at[color=green]
              > > design time.
              > >
              > > As for the runtime behaviour, don't expect to be able to cast anything,[/color]
              > more[color=green]
              > > that i could do something like IData.MyStaticM ethod(). I don't know that
              > > this is so odd either. Many data layer class are implemented statically[/color]
              > and[color=green]
              > > it would be nice to able to have some reliable interface to make sure[/color][/color]
              the[color=blue][color=green]
              > > static method i want to call exists!
              > >
              > > "John Wood" <jwood8@go_ahea d_remove_this.o ptonline.net> wrote in message
              > > news:bsuZa.2771 7$_R5.7863324@n ews4.srv.hcvlny .cv.net...[color=darkred]
              > > > So even if you could put static members in an interface and implement,[/color][/color]
              > how[color=green][color=darkred]
              > > > would that be useful?
              > > >
              > > > Are you going to be passing around the Type of the class, and somehow[/color]
              > > you'd[color=darkred]
              > > > expect to be able to cast that type to an interface?
              > > >
              > > > Seems a bit odd.
              > > >
              > > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote in[/color]
              > > message[color=darkred]
              > > > news:u6Rhgu1XDH A.1204@TK2MSFTN GP12.phx.gbl...
              > > > > Right. Static methods can't be part of the interface definition[/color][/color]
              > though.[color=green][color=darkred]
              > > > Any
              > > > > reason?
              > > > >
              > > > > If i write a data layer (as i am doing...) it would be nice to[/color][/color][/color]
              define[color=blue]
              > an[color=green][color=darkred]
              > > > > interface that my class can then implement using static methods,[/color][/color]
              > rather[color=green][color=darkred]
              > > > than
              > > > > instance methods.
              > > > >
              > > > > "John Wood" <j@ro.com> wrote in message
              > > > > news:uUVSPp1XDH A.3248@tk2msftn gp13.phx.gbl...
              > > > > > You mean *implement* the methods of an interface with a static[/color]
              > > modifier?[color=darkred]
              > > > > > Obviously the signature has to match. Or perhaps you mean having[/color]
              > > statics[color=darkred]
              > > > > as
              > > > > > part of the interface definition?
              > > > > >
              > > > > > Either way, interfaces are about defining a contract of method[/color][/color][/color]
              names[color=blue][color=green][color=darkred]
              > > > that
              > > > > an
              > > > > > *instance* has to support, statics aren't instance level methods[/color]
              > > they're[color=darkred]
              > > > > > class level.
              > > > > >
              > > > > > "Steven Livingstone" <s.livingstone@ nospam.btintern et.com> wrote[/color][/color][/color]
              in[color=blue][color=green][color=darkred]
              > > > > message
              > > > > > news:%23806ck1X DHA.2200@TK2MSF TNGP09.phx.gbl. ..
              > > > > > > Anyone able to explain to me why you cannot define an interface[/color][/color]
              > that[color=green][color=darkred]
              > > > can
              > > > > > > then be implemented using static methods?
              > > > > > >
              > > > > > > I understand the C# CLS states this, but just interested in the
              > > > reasons
              > > > > > > behind it.
              > > > > > >
              > > > > > > thanks,
              > > > > > > Steven
              > > > > > >
              > > > > > >
              > > > > >
              > > > > >
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • John Wood

                #8
                Re: Interface definition for static methods

                Yeah that's an interesting idea (extending it to constructors). You could
                extend the interface syntax to support constructors quite easily, eg.

                interface IPlugin
                {
                this();
                void RunPlug();
                ...
                }

                Personally I'd see that as more useful and easier for the C# compiler team
                to implement than interfaces supporting static members.

                "Jon Skeet" <skeet@pobox.co m> wrote in message
                news:MPG.19a0a1 d5df28448098a2c 8@news.microsof t.com...[color=blue]
                > John Wood <jwood8@go_ahea d_remove_this.o ptonline.net> wrote:[color=green]
                > > I can understand that you may want to provide a good way to ensure that[/color][/color]
                a[color=blue][color=green]
                > > class provides a specific set of static members, but other than that I[/color][/color]
                can't[color=blue][color=green]
                > > see the use. You could possibly implement that type of validation using[/color][/color]
                a[color=blue][color=green]
                > > custom attribute.[/color]
                >
                > That is indeed the only appropriate reason, but it *can* be a good
                > reason - including for constructors. For instance, it can be very
                > useful to require that a plugin class must have a parameterless
                > constructor so that the plugin loader can instantiate it.
                >
                > I don't see how custom attributes really help in this case, to be
                > honest - they certainly don't help to catch things at compile time.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet/
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet

                  #9
                  Re: Interface definition for static methods

                  John Wood <jwood8@spaam.o ptonline.net> wrote:[color=blue]
                  > Yeah that's an interesting idea (extending it to constructors). You could
                  > extend the interface syntax to support constructors quite easily, eg.
                  >
                  > interface IPlugin
                  > {
                  > this();
                  > void RunPlug();
                  > ...
                  > }
                  >
                  > Personally I'd see that as more useful and easier for the C# compiler team
                  > to implement than interfaces supporting static members.[/color]

                  It's much the same thing, I'd have thought.

                  The slightly strange thing is that as far as the CLR is concerned, an
                  interface *can* have static members.

                  I think it would be a bad idea to change the syntax to support one of
                  the two, but not the other - the reasons for including them are
                  basically the same.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  Working...