Bug 5217 – Permit static+abstract

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2010-11-14T12:41:13Z
Last change time
2018-10-19T02:41:08Z
Assigned to
No Owner
Creator
Austin Hastings

Comments

Comment #0 by ah08010-d — 2010-11-14T12:41:13Z
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 by issues.dlang — 2010-11-14T16:21:20Z
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 by ah08010-d — 2010-11-16T16:00:37Z
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 by issues.dlang — 2010-11-16T17:01:57Z
Do template constraints not work for you in this case?
Comment #4 by ah08010-d — 2010-11-16T18:53:00Z
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 by issues.dlang — 2010-11-16T19:42:08Z
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 by pro.mathias.lang — 2018-10-19T02:41:08Z
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/