Mandatory static methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ranier Dunno

    #1

    Mandatory static methods

    Hi,

    I am wondering if there is some construct to force a
    class to implement a set of static methods? Sort of like
    a "class interface" as opposed to the common "object
    interface"?
  • Jon Skeet

    #2
    Re: Mandatory static methods

    Ranier Dunno <ebuffer@hotmai l.com> wrote:[color=blue]
    > I am wondering if there is some construct to force a
    > class to implement a set of static methods? Sort of like
    > a "class interface" as opposed to the common "object
    > interface"?[/color]

    No. It's a common question, but there's no way of forcing either
    constructors or static methods to be implemented. The reason is that
    these methods/constructors would have to be called by reflection
    anyway, but it would be handy to be able to get the compiler to check
    that a well known method, or a specific constructor was implemented.

    --
    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

    • Ranier Dunno

      #3
      Re: Mandatory static methods

      Ok, that's what I thought, thanks. The reason I was
      asking, is actually that I want to add some "reflection-
      like" properties to a set of objects I have. My objects
      have properties which have values in a given scientific
      unit, and I want to provide the ability to query a class
      about its properties and the scientific units of those
      properties. Since all objects of the same class will have
      identical properties and units, it made sense to make the
      methods static (plus I don't want larger objects).

      Comment

      • Jon Skeet

        #4
        Re: Mandatory static methods

        Ranier Dunno <ebuffer@hotmai l.com> wrote:[color=blue]
        > Ok, that's what I thought, thanks. The reason I was
        > asking, is actually that I want to add some "reflection-
        > like" properties to a set of objects I have. My objects
        > have properties which have values in a given scientific
        > unit, and I want to provide the ability to query a class
        > about its properties and the scientific units of those
        > properties. Since all objects of the same class will have
        > identical properties and units, it made sense to make the
        > methods static (plus I don't want larger objects).[/color]

        I'm not entirely clear on what you're doing here, but you may want to
        consider using custom attributes for some of this - for instance, the
        units that the class uses could be implemented as a type-level
        attribute.

        --
        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

        • Ranier Dunno

          #5
          Re: Mandatory static methods

          >I'm not entirely clear on what you're doing here, but
          you may want to[color=blue]
          >consider using custom attributes for some of this - for[/color]
          instance, the[color=blue]
          >units that the class uses could be implemented as a type-[/color]
          level[color=blue]
          >attribute.[/color]

          OK, brilliant! I'll look into that - I am definitely
          looking for a more elegant way of doing things. In
          essence, I want to do this: say, I have a "Sprint" class
          representing short runs made by a single person on a
          track field. A "Sprint" will of course have some non-
          static properties, like the length of the sprint, name of
          the runner, time spent, average velocity and so forth.
          What I would like to do, is to add some meta-data for
          those properties. For instance, I would like the ability
          to specify that length is measured in meters, not in
          feet, and that velocity is meters/second, not mph.

          Comment

          • Jon Skeet

            #6
            Re: Mandatory static methods

            Ranier Dunno <ebuffer@hotmai l.com> wrote:[color=blue]
            > OK, brilliant! I'll look into that - I am definitely
            > looking for a more elegant way of doing things. In
            > essence, I want to do this: say, I have a "Sprint" class
            > representing short runs made by a single person on a
            > track field. A "Sprint" will of course have some non-
            > static properties, like the length of the sprint, name of
            > the runner, time spent, average velocity and so forth.
            > What I would like to do, is to add some meta-data for
            > those properties. For instance, I would like the ability
            > to specify that length is measured in meters, not in
            > feet, and that velocity is meters/second, not mph.[/color]

            Right. I would have attributes such as:

            [LengthUnit(Leng thUnits.Metre)]
            [TimeUnit(TimeUn its.Second)]

            either at a type or method level. (I'm not comfortable with the names
            above, as LengthUnits suggests it's a flag-based enum, but...)

            --
            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

            • Ranier Dunno

              #7
              Re: Mandatory static methods

              [color=blue]
              >Right. I would have attributes such as:
              >
              >[LengthUnit(Leng thUnits.Metre)]
              >[TimeUnit(TimeUn its.Second)]
              >
              >either at a type or method level. (I'm not comfortable[/color]
              with the names[color=blue]
              >above, as LengthUnits suggests it's a flag-based enum,[/color]
              but...)

              I'll go for that, it's just what the doctor ordered! :-)
              Thanks a lot, I was completely unaware of that C# feature
              (I'm a converted Java programmer...).

              Comment

              Working...