Article: Why Const Sucks

Meta jared771 at gmail.com
Tue Mar 6 14:51:27 UTC 2018


On Tuesday, 6 March 2018 at 10:02:10 UTC, Radu wrote:
> On Monday, 5 March 2018 at 10:57:35 UTC, Jonathan M Davis wrote:
>> Here's something I wrote up on const:
>>
>> http://jmdavisprog.com/articles/why-const-sucks.html
>>
>> I suppose that it's not exactly the most positive article, but 
>> I feel that it's accurate.
>>
>> - Jonathan M Davis
>
> Spot on article, and touches some of my pain points when 
> working with const/immutable structs.
>
> Recently I tried to create a ref-counted immutable struct, oh 
> boi...
>
> This later use case is of tremendous value for safe concurrent 
> code that's @nogc. Unfortunately I couldn't find a way to make 
> it work efficiently and in the same time not look like a 
> disgusting hack.
>
> I suspect a possible solution is to allow immutable(const) 
> postblit overloads as well as immutable dtors that will act as 
> an escape hatch for unsafe work and in the same time provide 
> hints that you are operating on an immutable(const) this.

AFAIK this is a solved problem. Dicebot (not sure if he's still 
around) had initially proposed it: store the reference count just 
before the immutable piece of memory.

| ref count |    object/struct    |

In fact, Andrei added a new allocator to 
std.experimental.allocator to support this:

https://dlang.org/library/std/experimental/allocator/building_blocks/affix_allocator/affix_allocator.prefix.html


More information about the Digitalmars-d-announce mailing list