April 05

On Saturday, 5 April 2025 at 21:39:01 UTC, Paul Backus wrote:

>

Not everyone will use opNew either. If you are capable of achieving uniformity with opNew, you are also capable of achieving uniformity without it.

Like I said, we can try to convince everyone to use 'make' as the one and only synonym for 'new', but when you offer an operator overload with nicer syntax, people naturally gravitate towards that. I've never seen someone create an array that you have to index with array.index(i) in D.

>

In any case, "writing parentheses is annoying sometimes" is kind of a weak justification for introducing new syntax.

Huh? Making code nicer to read/write is the whole point of syntax. It's why Lisp has its reputation, and why D allows make!T instead of make!(T), and why some propose to make parentheses around if conditions optional.

>

I cannot think of a single situation where I've been reading D code and felt the need to "easily locate creation of objects."

That's understandable, many programmers don't (need to) care where memory allocations happen.

>

I have been in situations where I wanted to know where a specific object was being created, and grepped for new T to find it--but I could have grepped for make!T just as easily.

But would someone who doesn't know about all the allocator libraries in use by a project know to include make!T in their search, as well as alloc!T etc.?

>

So, let me get this straight: you're proposing that myAllocator.new ubyte[n] would (potentially) return some custom library type, instead of a ubyte[]?

No, not at all. opNew should always return the same type as the equivalent new expression, I should probably add to the DIP that the compiler verifies that.

To illustrate what I'm talking about, here's the actual git history of a line of code in an OpenGL application of mine:

lightBias = newArrayMalloced!float(w * h);

lightBias = newArrayM!float(w * h);

lightBias = allocator.array!float(w * h);

lightBias = new float[](w * h);

You can tell I've been struggling with my memory allocator APIs. However, I've never struggled with the API for my custom allocator-backed array types, because opIndex and opBinaryAssign!"~" are the obvious choice which make refactoring from int[] to Array!int easy. Had opNew existed, I would have used that from the start and be content with it.

Yes, this might just be a very 'me' problem. And again, it's no big deal to rename newArrayMalloced to newArrayM in 35 places. I just thought this DIP could be a little nicety, filling a small hole in D's operator overloading story. I posted this DIP here to poll if anyone else is interested in this.

April 05

On Saturday, 5 April 2025 at 21:39:01 UTC, Paul Backus wrote:

>

On Saturday, 5 April 2025 at 19:30:49 UTC, Dennis wrote:

>

It's about being standard/uniform and recognizable. Not everyone uses make.

Not everyone will use opNew either. If you are capable of achieving uniformity with opNew, you are also capable of achieving uniformity without it.

>

Having to parenthesize array types like make!(int[]) is annoying, so you also have makeArray!int, others use alloc, or alloc.array!int etc.

I don't think those are any uglier than new int[](n), and they're certainly less confusing than new int[n].

In any case, "writing parentheses is annoying sometimes" is kind of a weak justification for introducing new syntax.

>

When scanning D code, I can easily locate creation of objects with new syntax. Calls to make!T or constructNew!T don't stand out from other functions like read!T or parse!T.

I cannot think of a single situation where I've been reading D code and felt the need to "easily locate creation of objects."

I have been in situations where I wanted to know where a specific object was being created, and grepped for new T to find it--but I could have grepped for make!T just as easily.

>

In D, I love how you can use operator overloading to create library arrays that are almost drop-in replacements for language arrays. It helps when incrementally refactoring GC-based array code to use custom allocators for performance / portability (to WebAssembly in my case).

I thought it would be nice to expand this library array type love to the new operator when allocating fixed size arrays, for example auto pixels = new ubyte[4 * width * height];.

So, let me get this straight: you're proposing that myAllocator.new ubyte[n] would (potentially) return some custom library type, instead of a ubyte[]?

That's...well, there are a lot of words I could use to describe it, but let's just say that "uniform" and "consistent" would not be among them.

Just tossing in my two cents, that when I first came to D (having been from a C++ background) one of the earlier things I tried to do was overload 'new' and was sad it was missing.

Having this overload can be really useful for instrumentation to track allocations and just have them all stem from 'new'. One use case I did this was for various factory functions I had in a game. I was able to overload 'new' temporarily and 'count' how often objects lived, (and with overloaded delete) see how long those objects lived. This helped me then make an object pool of a fixed size later (based on maximum allocations observed) to recycle objects from. For gaming applications

The other use case I have used for 'new' and 'delete' overloads in C++ is for a simple memory leak tracker, for counting and building a map of allocations.

Indeed, now I am currently writing my own 'alloc' function, but the consistency would be very useful to me as someone who wants to write more linters/tooling (and even just for having extra debug on some of my containers library). Having folks use opNew versus alloc/make/create/spawn/heapAlloc/etc. would be quite nice!

I suspect looking at Odin's 'contexts' for their allocators may provide more motivation: https://odin-lang.org/docs/overview/#allocators. I like the motivation here to be able to swap in and out allocators and have standard operator overloads for new and/or destroy. 'with' maybe seems like the tool here, but I need to think about it some more.

April 06

On Saturday, 5 April 2025 at 23:25:17 UTC, Mike Shah wrote:

>

Just tossing in my two cents, that when I first came to D (having been from a C++ background) one of the earlier things I tried to do was overload 'new' and was sad it was missing.

I think you misunderstand the OP. This is not at all similar to C++ new overloading. What C++ has is deprecated in D: https://dlang.org/deprecate.html#Class%20allocators%20and%20deallocators.

It is merely proposing syntactic sugar that changes allocator.new T into allocator.opNew!T. The functionality is already possible today with clear unambiguous syntax. Therefore in my opinion the proposal is not and improvement. It makes the language worse by adding new learning burden of an ambiguous* syntax.

-Johan

*ambiguous because of sharing same syntax as explicit instantiation of a nested class, as mentioned by OP.

April 07

On Sunday, 6 April 2025 at 13:22:06 UTC, Johan wrote:

>

On Saturday, 5 April 2025 at 23:25:17 UTC, Mike Shah wrote:

>

Just tossing in my two cents, that when I first came to D (having been from a C++ background) one of the earlier things I tried to do was overload 'new' and was sad it was missing.

I think you misunderstand the OP. This is not at all similar to C++ new overloading. What C++ has is deprecated in D: https://dlang.org/deprecate.html#Class%20allocators%20and%20deallocators.

It is merely proposing syntactic sugar that changes allocator.new T into allocator.opNew!T. The functionality is already possible today with clear unambiguous syntax. Therefore in my opinion the proposal is not and improvement. It makes the language worse by adding new learning burden of an ambiguous* syntax.

-Johan

*ambiguous because of sharing same syntax as explicit instantiation of a nested class, as mentioned by OP.

I see -- indeed overloading 'new' seems to be a deprecation -- thanks for sharing that (That is actually the kind of thing I would want to have, but free functions are also fine)!

I understand the proposal a bit more now and need to think on it from the standpoint of consistency.

1 2
Next ›   Last »