D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5217 - Permit static+abstract
Summary: Permit static+abstract
Status: RESOLVED WONTFIX
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-14 12:41 UTC by Austin Hastings
Modified: 2018-10-19 02:41 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Austin Hastings 2010-11-14 12:41:13 UTC
The abstract keyword currently cannot be used with "non virtual" functions. IMO, this is an error. 
 
Non-virtual simply means that only one layer of implementation will be possible - since a vtable would be required to disambiguate multiple layers. It does not mean that the implementation is automatically obvious, nor does it mean that multiple layers of interfaces cannot be composed into the single layer of implementation.
 
Factory methods, for example, are usually static. And in the presence of templates, the expectation should be for a high degree of fan-out, but not much depth. (That is, a template can be realized with many type parameters, but is less likely to be stacked up in a deep inheritance tree, although it does happen.)

Thus, I would very much like to be able to require a static method, via the abstract keyword.
Comment 1 Jonathan M Davis 2010-11-14 16:21:20 UTC
I'm afraid that you're going to have to be more clear in what you mean.

abstract indicates that a function does not have a body and that derived types must give an implementation if they are to be instantiatable.

static functions have nothing to do with the virtual table, and so they don't care about base or derived types. They're essentially just namespaced within a class or struct. abstract has no meaning for them.

Templates _always_ result in code. What does it matter if that code is a static function, within a class, a struct, or whatever? An abstract template makes no sense. Even if you're instantiating a template within a base class, it must be fully instantiated within that class regardless of whether any classes will ever be derived from that class.

What are you trying to do here? You need to be clearer, because as it stands, what you're asking for makes no sense.
Comment 2 Austin Hastings 2010-11-16 16:00:37 UTC
What I'm asking for is "abstract static". That is, a syntax for imposing a requirement that a class (or struct) provide a static member function before it can be instantiated.
 
In my particular use case, I've got a template and would like to impose a requirement for a factory method on any realizations of the template.
Comment 3 Jonathan M Davis 2010-11-16 17:01:57 UTC
Do template constraints not work for you in this case?
Comment 4 Austin Hastings 2010-11-16 18:53:00 UTC
Template constraints do not work. The problem is not "The input type must behave in such-and-such manner," but rather "Here is a template for most, but not all, of the class: you must provide two static methods that I have marked as abstract."
Comment 5 Jonathan M Davis 2010-11-16 19:42:08 UTC
Hmm. I think that I get what you're getting at, though it is a bit odd. Certainly, requiring static functions is something different. I've never seen a language do that. I'd still think that you could do it with a static if or template constraint, but without seeing the code, I'm not sure that it's clear enough exactly what you're trying to for me to give any kind of real suggestion about how to g about it with D as it is.

In general, I don't think that I support the idea of enforcing that a type have a particular set of static functions, but it does seem like a perfectly legitimate enhancement request. I'm inclined to think that you should probably be doing things a different way. But I'm not familiar with your code, so I obviously can't say for sure.
Comment 6 Mathias LANG 2018-10-19 02:41:08 UTC
Since this is a debatable enhancement request, and hasn't seen much activity for a while, I'm going to close it and suggest opening a DIP if you want to pursue this.
https://github.com/dlang/DIPs/