From slices to perfect imitators: opByValue

sclytrack via Digitalmars-d digitalmars-d at puremagic.com
Fri May 9 13:36:52 PDT 2014


On Thursday, 8 May 2014 at 11:05:20 UTC, monarch_dodra wrote:
> On Thursday, 8 May 2014 at 07:09:24 UTC, Sönke Ludwig wrote:
>> Just a general note: This is not only interesting for 
>> range/slice types, but for any user defined reference type 
>> (e.g. RefCounted!T or Isolated!T).
>
> Not necessarily: As soon as indirections come into play, you 
> are basically screwed, since const is "turtles all the way 
> down".
>
> So for example, the conversion from "const RefCounted!T" to 
> "RefCounted!(const T)" is simply not possible, because it 
> strips the const-ness of the ref count.
>
> What we would *really* need here is NOT:
> "const RefCounted!T" => "RefCounted!(const T)"
> But rather
> "RefCounted!T" => "RefCounted!(const T)"
>
> The idea is to cut out the "head const" directly. This also 
> applies to most ranges too BTW.

Skip paragraph.
Okay daedalnix. Second attempt. Started with 
Container!(const(T)). Thought about
separating the the const. Container!(T, const) and then only one 
const. None of
that Container!(A,B, immutable, const). Then thought about int 
qual(*) * a.
With qual as entry point. Then decided to go tail const only, 
single head mutable. But then seeing that there are two cases 
above, decided to
go acceptor, copy.
-------------------

a) I'm going to call this the copying case where the value types 
are copied
> "const RefCounted!T" => "RefCounted!(const T)"
immutable int [] a  => immutable (int) [] a;
immutable to mutable

b) Acceptor case.
> "RefCounted!T" => "RefCounted!(const T)"
e.g a const field accepts the mutable or immutable field.


-------------------

struct DemoStruct
{
   int * * a;
   acceptor int * * b;

   void demonstrate() acceptor
   {
     assert(typeof(a).stringof == "int * *");
     assert(typeof(b).stringof == "acceptor(int *) *");
   }
}


void test(acceptor(const) DemoStruct v)
{
   assert(typeof(v.a).stringof == "int * *");
   assert(typeof(v.b).stringof == "const(int *) *");
}


void main()
{
   DemoStruct m;
   test(m);
   acceptor(immutable) i;
   test(i);
}


Like having acceptor behave like inout or something. The acceptor 
field can
receive the following.

int * * a;
immutable (int *) * b;

const(int *) * acceptorfield = a;
acceptorfield = b;

const(int) * * oops = b; // not valid acceptor.
const(int * *) meh = b;  // will choose the first pointer mutable 
since
			//  is a copy so meh.

The acceptor field is tail const or something with the first 
entry being mutable.

-------------------

struct DemoStruct
{
   int * * a;
   acceptor int * * b;

   void demonstrate() copy
   {
     assert(typeof(a).stringof == "copy(int *) *");
     assert(typeof(b).stringof == "copy(int *) *");
   }
}


void test(copy(const) DemoStruct v)
{
   assert(typeof(v.a).stringof == "const(int *) *");
   assert(typeof(v.b).stringof == "const(int *) *");
}

void main()
{
   immutable DemoStruct i;
   test(i);
}


For the copying version, immutable ==> mutable the acceptor is 
applied to all
fields.



Please forgive me for pressing the send button.

Sclytrack


More information about the Digitalmars-d mailing list