refInt = ref int: how to achieve this? or is this a bug?

Stanislav Blinov stanislav.blinov at gmail.com
Thu Jun 18 00:40:15 UTC 2020


On Wednesday, 17 June 2020 at 23:58:37 UTC, mw wrote:
> On Wednesday, 17 June 2020 at 23:48:52 UTC, Stanislav Blinov 
> wrote:
>>> f(    T) /*more static if here*/ { /*potential dup code 
>>> here*/ }
>>> f(ref T) /*more static if here*/ { /*potential dup code 
>>> here*/ }
>>>
>>> I would call for improvement.
>>
>> Again, looking at your code, this all comes out of your trying 
>> to conflate references and pass by reference. References (i.e. 
>> classes) and values (i.e. primitive types and structs) are 
>> semantically different, so should be treated differently.
>
> It has to be done: because the C lib only accept void* as value.
>
> So on the D side, need to pass to C:
>
> -- primitive types, class (essentially pointers) by value
> -- struct, union, array|string, by reference
>
> The discussion here is how to make this passing code simple to 
> write.

That's completely backwards. Because the C lib only takes void*, 
it forfeits any type safety and memory safety, so it is *on you* 
to preserve that. Which would require quite some code indeed. I 
don't think you've fully considered the implications of your code.

You allocate an opaque memory region, which the GC knows nothing 
about, and you want to put pointers and references into it. 
That's bug number 1. Whether the GC *should* know about that 
memory depends on types you're using - static ifs are a must.

You want to escape unshared pointers into other threads. That's 
bug number 2. The language can't statically verify that a pointer 
you're passing is the only pointer. It can only help you verify 
that a pointer you're passing may not point to unshared data - 
static ifs are a must.

You want to escape pointers to locals on the stack to other 
threads. That's, tentatively, bug number 3. And no static ifs for 
that one, here you're on your own.

And you want it all doable with "simple to write" code? That code 
*should* be onerous to write, like any other side stepping of the 
type system.


More information about the Digitalmars-d mailing list