abstract static?

  • Thread starter Thread starter Chris Capel
  • Start date Start date
Jonathan Malek said:
Daniel,
Correct me if I am wrong, but there is no polymorphic way to access static
methods in C# because C# doesn't support virtual static methods. Other
languages do (Delphi, for one). In other words, the most important argument
for not having abstract statics is that C# doesn't support them. So, I
figure I am missing something.
I have read quite a bit on this topic in other lists (the comp.std.c++
list had what seems like a semi-annual celebration on the issue for several
years), trying to understand what the issue is. I suppose I don't quite see
the messy aspect of it all. Everyone raises the same issue--"virtual
static?? Why ever would you want to do such a thing? Oh I see--well, you
know, you could do that this way, or that way..." That's not really an
answer. It can be done, it solves a particular design problem (I've written
one up below that I feel can only be solved this way--or, as you mention,
with contract-enforced attributes), and other languages have shown that.
So, is there something beneath it all, say in the CIL/CLS/CTS/CLR that
prohibits such a thing? Better question--is it possible to introduce
CTS-compliant virtual statics? I suppose if the answer to that is no, then
it is pointless wondering why C# doesn't support them.

Well, the primary problem is in cases like this, assume Base has a virtual
static called StaticMethod and Derived is derived from Base.

Base b = new Derived();
//this really isn't legal, because you can't access a static method via a
//reference(nor should you be able to, imho).
//even if you could, which StaticMethod is being called here? cases could be
//made that it should be Base.StaticMethod() or Derived.StaticMethod()
//if Derived.StaticMethod() is proper, you could end up with unpredictable
//behaviour here.
b.StaticMethod();

How exactly do you execute the static method without exposing statics (and
some very ugly and troublesome code constructs)? The only way I could think
of currently would be using reflection.
I do not think that there is a technical reason they cannot be added, so
much as a semantic one. Its hard to add such a construct in a way that keeps
the language simple. As such, I don't think it would be a clean thing to add
to the CLR, as it would vastly complicate C# and VB, either by adding a new
keyword(I could see something like staticcall b.StaticMethod() or the like,
its ugly though) or by merging static members into the instance member list,
which is also very ugly. I also do not know what kind of a performance issue
it could be.
I really think a contract construct that can specify attributes and
constructors would provide a better solution, especially as the IDE's
mature.
Jonathan Malek said:
I traced through the responses to the original question, and while I
might have missed something, I feel Chris Capel's question was never
really answered. There were responses about how to do what Chris
wants to do differently, responses that argued the idea, even a nod to
contracts--which I thought missed the whole point.

In the example given, a contract has already been established--in the
form of an abstract class.

I have my own design issue, which brought me to this group in the
first place. I am working on a large number of domain and data mapper
pattern objects. Some of my mappers can handle more than one domain
object, because the domain objects are subclassed.

In order to use a mapper registry (factory) to produce them, I decided
to have each mapper track its mapped types:

public abstract class BaseDataMapper
{
public abstract Type[] MappedTypes{ get ; }
...
}

I think it is reasonable that the author of a mapper should decide
which objects that mapper can map into persistent storage:

public class NamedObjectMapper : BaseDataMapper
{
public override Type[] MappedTypes
{
get{ return new Type[]{ typeof( ServiceType ), typeof(
UnitType ) } ; )
}
}

Now, during registration of the above mapper type in a factory, I need
some way of registering the supported types--from:

mapperRegistry.GetInstanceFor( ServiceType )

I expect the factory to produce the exact mapper I need. Unless the
property described above is also abstract, I cannot do this without
creating an instance of the mapper during registration. Five mappers?
No big deal. Four hundred mappers in a service layer? No way I want
to create an instance of all of those during registration. Not in an
environment where new domain objects and mappers can be added at
runtime, and their types registered.

And this is the thing: attributes? Come on. The issue here is to
force the mapper author to comply with the contract.

So--why not abstract statics?
Because they are messy, if you read everything posted there were a number of
arguments. The most important one is that there is no polymorphic way to
access static methods, you are forced to use reflection and any feature that
REQUIRES reflection to use it is not something that should exist, IMHO.
I would not ever use statics in that manner, attributes exist for just that
purpose(among others, of course). I do however think that a contract that
can handle required attributes should exist, that is a very different matter
entirely.
"Chris Capel" <[email protected]> wrote in message
What is the rationale behind the decision not to allow abstract
static
class
members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible. It
seems
like
it would be useful. In fact, there's a place in my code that I could make
good use of it. So why not?

Chris
 
Right--the issue of making class calls from an instance (which I believe Java allows). I agree, not a friendly process, and definitely error prone.
Do you know of a list that's tracking the contact constructs you mention? I would be interested in watching that progress. Thanks for clearing things up for me.
Jonathan Malek said:
Daniel,
Correct me if I am wrong, but there is no polymorphic way to access static
methods in C# because C# doesn't support virtual static methods. Other
languages do (Delphi, for one). In other words, the most important argument
for not having abstract statics is that C# doesn't support them. So, I
figure I am missing something.
I have read quite a bit on this topic in other lists (the comp.std.c++
list had what seems like a semi-annual celebration on the issue for several
years), trying to understand what the issue is. I suppose I don't quite see
the messy aspect of it all. Everyone raises the same issue--"virtual
static?? Why ever would you want to do such a thing? Oh I see--well, you
know, you could do that this way, or that way..." That's not really an
answer. It can be done, it solves a particular design problem (I've written
one up below that I feel can only be solved this way--or, as you mention,
with contract-enforced attributes), and other languages have shown that.
So, is there something beneath it all, say in the CIL/CLS/CTS/CLR that
prohibits such a thing? Better question--is it possible to introduce
CTS-compliant virtual statics? I suppose if the answer to that is no, then
it is pointless wondering why C# doesn't support them.

Well, the primary problem is in cases like this, assume Base has a virtual
static called StaticMethod and Derived is derived from Base.

Base b = new Derived();
//this really isn't legal, because you can't access a static method via a
//reference(nor should you be able to, imho).
//even if you could, which StaticMethod is being called here? cases could be
//made that it should be Base.StaticMethod() or Derived.StaticMethod()
//if Derived.StaticMethod() is proper, you could end up with unpredictable
//behaviour here.
b.StaticMethod();

How exactly do you execute the static method without exposing statics (and
some very ugly and troublesome code constructs)? The only way I could think
of currently would be using reflection.
I do not think that there is a technical reason they cannot be added, so
much as a semantic one. Its hard to add such a construct in a way that keeps
the language simple. As such, I don't think it would be a clean thing to add
to the CLR, as it would vastly complicate C# and VB, either by adding a new
keyword(I could see something like staticcall b.StaticMethod() or the like,
its ugly though) or by merging static members into the instance member list,
which is also very ugly. I also do not know what kind of a performance issue
it could be.
I really think a contract construct that can specify attributes and
constructors would provide a better solution, especially as the IDE's
mature.
I traced through the responses to the original question, and while I
might have missed something, I feel Chris Capel's question was never
really answered. There were responses about how to do what Chris
wants to do differently, responses that argued the idea, even a nod to
contracts--which I thought missed the whole point.

In the example given, a contract has already been established--in the
form of an abstract class.

I have my own design issue, which brought me to this group in the
first place. I am working on a large number of domain and data mapper
pattern objects. Some of my mappers can handle more than one domain
object, because the domain objects are subclassed.

In order to use a mapper registry (factory) to produce them, I decided
to have each mapper track its mapped types:

public abstract class BaseDataMapper
{
public abstract Type[] MappedTypes{ get ; }
...
}

I think it is reasonable that the author of a mapper should decide
which objects that mapper can map into persistent storage:

public class NamedObjectMapper : BaseDataMapper
{
public override Type[] MappedTypes
{
get{ return new Type[]{ typeof( ServiceType ), typeof(
UnitType ) } ; )
}
}

Now, during registration of the above mapper type in a factory, I need
some way of registering the supported types--from:

mapperRegistry.GetInstanceFor( ServiceType )

I expect the factory to produce the exact mapper I need. Unless the
property described above is also abstract, I cannot do this without
creating an instance of the mapper during registration. Five mappers?
No big deal. Four hundred mappers in a service layer? No way I want
to create an instance of all of those during registration. Not in an
environment where new domain objects and mappers can be added at
runtime, and their types registered.

And this is the thing: attributes? Come on. The issue here is to
force the mapper author to comply with the contract.

So--why not abstract statics?

Because they are messy, if you read everything posted there were a number of
arguments. The most important one is that there is no polymorphic way to
access static methods, you are forced to use reflection and any feature that
REQUIRES reflection to use it is not something that should exist, IMHO.
I would not ever use statics in that manner, attributes exist for just that
purpose(among others, of course). I do however think that a contract that
can handle required attributes should exist, that is a very different matter
entirely.

What is the rationale behind the decision not to allow abstract static
class
members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible. It seems
like
it would be useful. In fact, there's a place in my code that I could
make
good use of it. So why not?

Chris
 
Jonathan Malek said:
Right--the issue of making class calls from an instance (which I
believe Java allows). I agree, not a friendly process, and definitely
error prone.

Although Java unfortunately allows it as syntax, it doesn't use the
type of the instance (or even whether or not it's null) at runtime. In
other words, you can do:

Foo x = new Bar();
x.Something();

and it will call Foo.Something() even if there's a Bar.Something()
method.
 
Jonathan Malek said:
Right--the issue of making class calls from an instance (which I believe
Java allows). I agree, not a friendly process, and definitely error prone.
Do you know of a list that's tracking the contact constructs you mention?
I would be interested in watching that progress. Thanks for clearing things
up for me.

Currently no lists tracking such a contract exists to my knowledge(nor does
any work on such a one exist, AFAIK). I have floated the idea in various
basic forms here a few times with little more results than things like "That
may be useful" or "I could see how that would work" kind of things. I'm
still considering writing up a full fledged proposal & syntax and posting it
here for comments, but time constraints and reaction to the various ideas
I've posted here havn't pushed me to get on it right away.
Jonathan Malek said:
Daniel,
Correct me if I am wrong, but there is no polymorphic way to access
static
methods in C# because C# doesn't support virtual static methods. Other
languages do (Delphi, for one). In other words, the most important argument
for not having abstract statics is that C# doesn't support them. So, I
figure I am missing something.
I have read quite a bit on this topic in other lists (the comp.std.c++
list had what seems like a semi-annual celebration on the issue for several
years), trying to understand what the issue is. I suppose I don't quite see
the messy aspect of it all. Everyone raises the same issue--"virtual
static?? Why ever would you want to do such a thing? Oh I see--well, you
know, you could do that this way, or that way..." That's not really an
answer. It can be done, it solves a particular design problem (I've written
one up below that I feel can only be solved this way--or, as you mention,
with contract-enforced attributes), and other languages have shown that.
So, is there something beneath it all, say in the CIL/CLS/CTS/CLR that
prohibits such a thing? Better question--is it possible to introduce
CTS-compliant virtual statics? I suppose if the answer to that is no, then
it is pointless wondering why C# doesn't support them.

Well, the primary problem is in cases like this, assume Base has a virtual
static called StaticMethod and Derived is derived from Base.

Base b = new Derived();
//this really isn't legal, because you can't access a static method via a
//reference(nor should you be able to, imho).
//even if you could, which StaticMethod is being called here? cases could be
//made that it should be Base.StaticMethod() or Derived.StaticMethod()
//if Derived.StaticMethod() is proper, you could end up with unpredictable
//behaviour here.
b.StaticMethod();

How exactly do you execute the static method without exposing statics (and
some very ugly and troublesome code constructs)? The only way I could think
of currently would be using reflection.
I do not think that there is a technical reason they cannot be added, so
much as a semantic one. Its hard to add such a construct in a way that keeps
the language simple. As such, I don't think it would be a clean thing to add
to the CLR, as it would vastly complicate C# and VB, either by adding a new
keyword(I could see something like staticcall b.StaticMethod() or the like,
its ugly though) or by merging static members into the instance member list,
which is also very ugly. I also do not know what kind of a performance issue
it could be.
I really think a contract construct that can specify attributes and
constructors would provide a better solution, especially as the IDE's
mature.
I traced through the responses to the original question, and while I
might have missed something, I feel Chris Capel's question was never
really answered. There were responses about how to do what Chris
wants to do differently, responses that argued the idea, even a nod to
contracts--which I thought missed the whole point.

In the example given, a contract has already been established--in the
form of an abstract class.

I have my own design issue, which brought me to this group in the
first place. I am working on a large number of domain and data mapper
pattern objects. Some of my mappers can handle more than one domain
object, because the domain objects are subclassed.

In order to use a mapper registry (factory) to produce them, I decided
to have each mapper track its mapped types:

public abstract class BaseDataMapper
{
public abstract Type[] MappedTypes{ get ; }
...
}

I think it is reasonable that the author of a mapper should decide
which objects that mapper can map into persistent storage:

public class NamedObjectMapper : BaseDataMapper
{
public override Type[] MappedTypes
{
get{ return new Type[]{ typeof( ServiceType ), typeof(
UnitType ) } ; )
}
}

Now, during registration of the above mapper type in a factory, I need
some way of registering the supported types--from:

mapperRegistry.GetInstanceFor( ServiceType )

I expect the factory to produce the exact mapper I need. Unless the
property described above is also abstract, I cannot do this without
creating an instance of the mapper during registration. Five mappers?
No big deal. Four hundred mappers in a service layer? No way I want
to create an instance of all of those during registration. Not in an
environment where new domain objects and mappers can be added at
runtime, and their types registered.

And this is the thing: attributes? Come on. The issue here is to
force the mapper author to comply with the contract.

So--why not abstract statics?

Because they are messy, if you read everything posted there were a number of
arguments. The most important one is that there is no polymorphic way to
access static methods, you are forced to use reflection and any
feature
that
REQUIRES reflection to use it is not something that should exist, IMHO.
I would not ever use statics in that manner, attributes exist for
just
that
purpose(among others, of course). I do however think that a contract that
can handle required attributes should exist, that is a very
different
matter
entirely.

What is the rationale behind the decision not to allow abstract static
class
members? It doesn't seem like it's a logically contradictory concept, or
that the implementation would be difficult or near-impossible.
It
seems
like
it would be useful. In fact, there's a place in my code that I could
make
good use of it. So why not?

Chris
 
I started reading this thread, because I was talking about something along
these lines at work today with a fellow developer. I have some functions
that are specific to one component of the application that we are
developing. These functions will be used by several classes however. His
suggestion was to make static functions in a new class.

I have come to realize that this thread is not exactly the same as what I
was thinking about. This thread, as far as I can tell, is discussing having
"abstract static members" in a class as opposed to having "public static
members" in an abstract class, which you can do. My reasoning behind having
the abstract class with public static functions was to prevent anyone from
creating an unnecessary object from the class. This may not be the best way
of doing what I was after, but it works.
 
Kerry Sanders said:
I started reading this thread, because I was talking about something along
these lines at work today with a fellow developer. I have some functions
that are specific to one component of the application that we are
developing. These functions will be used by several classes however. His
suggestion was to make static functions in a new class.

I have come to realize that this thread is not exactly the same as what I
was thinking about. This thread, as far as I can tell, is discussing having
"abstract static members" in a class as opposed to having "public static
members" in an abstract class, which you can do. My reasoning behind having
the abstract class with public static functions was to prevent anyone from
creating an unnecessary object from the class. This may not be the best way
of doing what I was after, but it works.

It works, but that allows the class to be derived from. Perhaps a sealed
class suits you better?
 
Thanks for the reply. I thought about that very thing after I posted.
Neither one is really the best option for my initial intentions. I was
after a way to symbolize to other developers that they did not have to
initiate an object instance of the class before using the functions it
contains. Does that make sense? I am very tired at the moment. :)

Anyway... I guess sealed is better than abstract. At least another
developer would not be able to change the desired intentions of the class.
 
Kerry Sanders said:
Thanks for the reply. I thought about that very thing after I posted.
Neither one is really the best option for my initial intentions. I was
after a way to symbolize to other developers that they did not have to
initiate an object instance of the class before using the functions it
contains. Does that make sense? I am very tired at the moment. :)
Yes, it does. However, documenting the class with a very obvious static
symbol should work as well as not providing a constructor. Intellisense will
do the rest.
Anyway... I guess sealed is better than abstract. At least another
developer would not be able to change the desired intentions of the class.
Ya, thats mostly why I'd use sealed unless there was a valid reason that the
class would need to be created. It saves on confusion if someone decides to
inherit from the class strictly for naming purposes or something(I've seen
it done).
 
Daniel O'Connell said:
Yes, it does. However, documenting the class with a very obvious static
symbol should work as well as not providing a constructor. Intellisense will
do the rest.

Note that if you just don't provide any constructors at all, a public
parameterless one will be provided for you. You might want to write a
private constructor, which means that nothing else can instantiate it.
 
Jon Skeet said:
Note that if you just don't provide any constructors at all, a public
parameterless one will be provided for you. You might want to write a
private constructor, which means that nothing else can instantiate it.
Sorry, that should have been public constructor, :-p
 
Back
Top