Bug 23680 – Support static classes in D

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2023-02-08T10:22:33Z
Last change time
2024-12-13T19:27:10Z
Assigned to
No Owner
Creator
Marcelo Silva Nascimento Mancini
Moved to GitHub: dmd#20228 →

Comments

Comment #0 by msnmancini — 2023-02-08T10:22:33Z
D must support the following syntax: ```d static class Abc { void print(){writeln("Print");} } ``` This should be expanded to: ```d class Abc { @disable this(); static: void print(){writeln("Print");} } ``` Rationale: (This *could* be a DIP, but I don't want it to get hidden from who's developing the compiler, plus that's a trivial request and I don't want to lost time again writing useless things) 1: Currently `static class` has no meaning and the compiler simply ignores, which can surprise a lot of newcomers as it *seems* valid code but it doesn't seem to work. 2: A lot of people comes from C# and Java to test D and they find it inexcusable to not have them. D does not have a namespace keyword and that behavior is not possible to simulate using a single file, so a class must be used. This should be trivial to implement and would be a beginner friend feature. Forum posts: [1]: https://forum.dlang.org/post/[email protected] [2]: https://forum.dlang.org/thread/[email protected]?page=1 Keep in mind that every now and then it appears people trying to do that on the D discord so this is overlooked all the time.
Comment #1 by default_357-line — 2023-02-08T13:27:20Z
Just in case you're not aware, you can do this: ``` module abc; import std.stdio; void print() { writeln("Hello"); } --- module test; static import abc; void main() { abc.print; } ``` or ``` module test; import abc = abc; void main() { abc.print; } ``` I think `static class` should error for toplevel declarations. It has an established meaning for nested classes: it removes the context pointer.
Comment #2 by msnmancini — 2023-02-08T13:32:07Z
Yea I'm aware but this is really not intuitive for beginners. Plus, there's no way to do that using a single file. Like, it's not really me, but People.Like.Doing.That.Way, always uppercase, specially from people coming from Java which they directly imported a class. Yea `static class` has no meaning for top level, so, for me, the solution to that is giving it a meaning by or implementing the static class functionality from other languages or giving it an error.
Comment #3 by robert.schadek — 2024-12-13T19:27:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20228 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB