Jump to page: 1 2
Thread overview
Preventing .init for Archive struct, Programming in D, page 295
15 hours ago
Brother Bill
15 hours ago
Monkyyy
15 hours ago
Brother Bill
15 hours ago
Monkyyy
12 hours ago
Brother Bill
11 hours ago
monkyyy
11 hours ago
H. S. Teoh
9 hours ago
monkyyy
7 hours ago
Brother Bill
7 hours ago
monkyyy
15 hours ago
Dejan Lekic
15 hours ago

Is is possible to 'disable' .init for a struct?

source/app.d

import std.stdio;

void main()
{
	// Can still create Archive with an empty filename. We can't have that.
	auto noDefault = Archive.init;
	writefln("fileName: [%s]", noDefault.fileName);
}

// adding a constructor automatically removes default constructor, so need to disable it.
struct Archive
{
	string fileName;

	@disable this();						// Cannot call default constructor
	@disable this(ref const(typeof(this)));	// Disable the copy constructor
	@disable this(this);					// Disable the postblit.  Always do this.
	@disable typeof(this) opAssign(ref const(typeof(this))); // Disable assignment operator

	this(string fileName) {	// Explicit constructor, can be called.
		this.fileName = fileName;
	}
}

Console output:

fileName: []
15 hours ago

On Friday, 12 September 2025 at 15:20:38 UTC, Brother Bill wrote:

>

Is is possible to 'disable' .init for a struct?

No (and honestly it's bad style to break the constructors)

15 hours ago

On Friday, 12 September 2025 at 15:24:46 UTC, Monkyyy wrote:

>

On Friday, 12 September 2025 at 15:20:38 UTC, Brother Bill wrote:

>

Is is possible to 'disable' .init for a struct?

No (and honestly it's bad style to break the constructors)

Please expound on why its 'bad style' to disable constructors.
Would you accept that disabling postblit may be the exception?

15 hours ago

On Friday, 12 September 2025 at 15:31:37 UTC, Brother Bill wrote:

>

On Friday, 12 September 2025 at 15:24:46 UTC, Monkyyy wrote:

>

On Friday, 12 September 2025 at 15:20:38 UTC, Brother Bill wrote:

>

Is is possible to 'disable' .init for a struct?

No (and honestly it's bad style to break the constructors)

Please expound on why its 'bad style' to disable constructors.
Would you accept that disabling postblit may be the exception?

Minor exceptions in type api creates hell inside nested metaprogramming and your clever scheme isnt going to be worth the trade off of making users learn your system for modifying the data.

Plain old data forever

15 hours ago

On Friday, 12 September 2025 at 15:20:38 UTC, Brother Bill wrote:

>

Is is possible to 'disable' .init for a struct?

You do not want .init?

Fine:

    Archive noInit = void;
12 hours ago

On Friday, 12 September 2025 at 15:38:15 UTC, Monkyyy wrote:

>

On Friday, 12 September 2025 at 15:31:37 UTC, Brother Bill wrote:

>

On Friday, 12 September 2025 at 15:24:46 UTC, Monkyyy wrote:

>

On Friday, 12 September 2025 at 15:20:38 UTC, Brother Bill wrote:

>

Is is possible to 'disable' .init for a struct?

No (and honestly it's bad style to break the constructors)

Please expound on why its 'bad style' to disable constructors.
Would you accept that disabling postblit may be the exception?

Minor exceptions in type api creates hell inside nested metaprogramming and your clever scheme isnt going to be worth the trade off of making users learn your system for modifying the data.

Plain old data forever

As a newbie to D, really trying to understand it, I am merely trying to uncover how to effectively use D. When Programming in D book, page 292, section 52.4 talks about 'postblit', it states: "If the postblit is defined for a type, the copy constructor is disabled." I can imagine that a D developer who forgot this 'legacy feature' might be frustrated when the expected copy constructor is disabled.

Is there a 'style' guide for D, such as: Do this, Don't do that, avoid this, etc.?
I recall Bertrand Meyer criticizing C++ by saying (paraphrased): "There are these features in the language, but you should avoid them, unless absolutely necessary", and then saying how many developers use language features when unnecessary?

I am trying to understand the clean way of using D. I would not expect that every D developer be expected to have read and understood every line of the D specification.
This seems to be something that a D developer should know about.

What do you recommend as the way for a newbie D developer to master the power of D, while avoiding going down the rabbit hole, and wondering why things don't work.

Most computer languages, such as Eiffel, don't even consider adding this feature of preventing construction.
Clearly, D has them for 'good' reasons.

Perhaps I am looking at D 'through the looking glass', rather through a better approach.

11 hours ago

On Friday, 12 September 2025 at 18:19:46 UTC, Brother Bill wrote:

>

Is there a 'style' guide for D, such as: Do this, Don't do that, avoid this, etc.?

Someone will link the "offical" style guide, but thats nothing id follow

>

"There are these features in the language, but you should avoid them, unless absolutely necessary", and then saying how many developers use language features when unnecessary?

Id avoid, annotations (all of them), delegates, classes, manual allocating extremists of all 3 flavors, multithreading (if you must, keep it functional and hope std.parrell works)

Solve your control flow in ranges, and if its a nightmare of a problem unit test them with plain old data types like ints.

>

I am trying to understand the clean way of using D.

I suggest chasing "Expressive" not clean

>

Most computer languages, such as Eiffel, don't even consider adding this feature of preventing construction.
Clearly, D has them for 'good' reasons.

Im pretty sure your just factually wrong in this case, I think disable this is well known accidental feature, and it caused allot of problems in the std trying to work around it. And well... thats not how you should relate with how the compiler is made in general; to this day walter doesnt do template metaprogramming and they were added halfway into the compilers life span(d was a c with classes compiler for 10 years before walter got annoyed with the c spec committy); and its still a new frontier in general.

The compiler isnt a clean well made work of art; its 30 years of hacks and experiments with several features being complete accidents; with 50/50 odds on being formalized. Templates in general are not understood, their turing completeness was an accident in c++.

11 hours ago
On Fri, Sep 12, 2025 at 06:19:46PM +0000, Brother Bill via Digitalmars-d-learn wrote: [...]
> As a newbie to D, really trying to understand it, I am merely trying to uncover how to effectively use D.  When Programming in D book, page 292, section 52.4 talks about 'postblit', it states: "If the postblit is defined for a type, the copy constructor is disabled."  I can imagine that a D developer who forgot this 'legacy feature' might be frustrated when the expected copy constructor is disabled.
> 
> Is there a 'style' guide for D, such as: Do this, Don't do that, avoid this, etc.?

The best way to learn D for a beginner is really just to start using it.  D was designed to be "correct by default", and IMO it succeeds in this for the most part.  This means that the most straightforward way to write something is almost always the correct way.


[...]
> I am trying to understand the clean way of using D.  I would not expect that every D developer be expected to have read and understood every line of the D specification.

Exactly.  And that is why you should just start writing D.  You will pick up the rest as you go along.


[...]
> What do you recommend as the way for a newbie D developer to master the power of D, while avoiding going down the rabbit hole, and wondering why things don't work.

For the most part, straightforward D code will Just Work(tm). Generally, the simpler it looks, the more likely it's correct.  If your code is starting to gather all sorts of paraphrenalia and it's starting to sprout hairs, it probably means you're not using D the way it was intended.


> Most computer languages, such as Eiffel, don't even consider adding
> this feature of preventing construction.
> Clearly, D has them for 'good' reasons.
[...]

If you want to know my honest opinion: don't use @disable at all.  It was added because some big D users clamored for it and possibly threatened to leave D otherwise, and Walter yielded.  IMNSHO it's a misfeature that goes against the grain of D.  This is why it has all sorts of troublesome and strange corner-case behaviours.  In the early days when it caused all sorts of problems with Phobos because just about every other function assumed .init exists and is a valid instance of a generic type T.  Generic code used .init everywhere to get an instance of any type the user may throw at it, and used this for temporary storage, introspection, adaptation, etc..  Life was good.  Everything worked in every combination, and everyone was happy.  (Well, *almost* everyone. :-P  Obviously, those who clamored for @disable had "very good" reasons for it.)  Then @disable showed up, and everybody's soup was ruined.  We spent years cleaning up the ensuing mess, and I doubt if even today we have finished cleaning everything up yet.  I'm certain almost you will find any number of (b0rken) interactions between @disable and other long-time language features if you looked hard enough.  If it were up to me, I'd avoid it like the plague.

(Unless the situation absolutely, indubitably, undeniably. and unavoidably calls for it, and there are no other workarounds at your disposal.)

Remember, D was designed to make D code look concise and pretty.  The most obvious way to write something is most likely the correct way to do it.  If a piece of D code looks like it fell in boilerplate mud and came out growing attribute hairs where there shouldn't be any, it's almost certain that it's either wrong, badly written, going against the way D was intended to be used, or all of the above.  (This applies to Phobos code as well, BTW.  Most of it is to work around misfeatures that got shoehorned into the language without being aware of the unintended interactions with other features.  In the early days, Phobos code was legendary for being a standard library that didn't make your head hurt when you read it. (If you've ever tried reading the source code for Glibc, or the standard library for almost any other language, really, you'll know what I mean.)  It was exemplary of how D code ought to be written.  It probably still is today to some extent, though sadly over the years it has fallen into the mud and come out hairy on multiple occasions, so it ain't as pretty as it used to be anymore.)


T

-- 
"640K ought to be enough" -- Bill G. (allegedly), 1984. "The Internet is not a primary goal for PC usage" -- Bill G., 1995. "Linux has no impact on Microsoft's strategy" -- Bill G., 1999.
9 hours ago
On Friday, 12 September 2025 at 20:10:25 UTC, H. S. Teoh wrote:
> Phobos code was legendary for being a standard library that didn't make your head hurt when you read it. (If you've ever tried reading the source code for Glibc, or the standard library for almost any other language, really, you'll know what I mean.)  It was exemplary of how D code ought to be written.  It probably still is today to some extent, though sadly over the years it has fallen into the mud and come out hairy on multiple occasions, so it ain't as pretty as it used to be anymore.)

> It probably still is today to some extent

*loud silence*

Its probably time to raise some standards


7 hours ago
On Friday, 12 September 2025 at 21:46:51 UTC, monkyyy wrote:
> On Friday, 12 September 2025 at 20:10:25 UTC, H. S. Teoh wrote:
>> Phobos code was legendary for being a standard library that didn't make your head hurt when you read it. (If you've ever tried reading the source code for Glibc, or the standard library for almost any other language, really, you'll know what I mean.)  It was exemplary of how D code ought to be written.  It probably still is today to some extent, though sadly over the years it has fallen into the mud and come out hairy on multiple occasions, so it ain't as pretty as it used to be anymore.)
>
>> It probably still is today to some extent
>
> *loud silence*
>
> Its probably time to raise some standards

Is it time to consider D 3.x.x, that takes the best of D, discards the mistakes, the mud and the hair, so most well written D 2.x.x programs still work?

Should it learn lessons from Rust, Zig, etc.

Should D follow Gleam, an Erlang variant in having functional programming with pattern matching, deconstruction, etc?

And take from Zig multiple ways to allocate memory?
« First   ‹ Prev
1 2