From slices to perfect imitators: opByValue

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu May 8 07:41:51 PDT 2014


On Thu, May 08, 2014 at 11:05:19AM +0000, monarch_dodra via Digitalmars-d 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.
> 
> We'd be much better of if we never used `const MyRange!T` to begin
> with, but simply had a conversion from `MyRange!T` to `MyRange!(const
> T)`, which references the same data.
> 
> In fact, I'm wondering if this might not be a more interesting
> direction to explore.

+1.

I don't like opByValue because it seems to be too special-cased. The
*real* issue we need to grapple with is head- vs. tail-const, and how to
interconvert between them in user-defined types. As others have already
pointed out, opByValue is really not that much different from opSlice
(arguably not different at all). It seems such a waste to spend so much
effort on it when we have much bigger fish to fry -- tail-const.


T

-- 
You are only young once, but you can stay immature indefinitely. -- azephrahel


More information about the Digitalmars-d mailing list