www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - IsValueType template? std.traits.hasAliasing?

reply Bill Baxter <wbaxter gmail.com> writes:
Does anyone have a good template for checking if a struct is
semantically a pure value type or not?
I found "hasAliasing" in D2 which is close, but it seems to report
true even for static arrays.

struct X { float[3] vals; }
pragma(msg, hasAliasing!(X)?"true":"false"); --> true

Maybe that's just a bug in hasAliasing?

What I want to know is if     X=Y will give me an X that is
independent of Y.  I think that is the same question hasAliasing is
trying to answer.  If so then it's a bug.

--bb
Jan 27 2009
next sibling parent reply dsimcha <dsimcha yahoo.com> writes:
== Quote from Bill Baxter (wbaxter gmail.com)'s article
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?
 I found "hasAliasing" in D2 which is close, but it seems to report
 true even for static arrays.
 struct X { float[3] vals; }
 pragma(msg, hasAliasing!(X)?"true":"false"); --> true
 Maybe that's just a bug in hasAliasing?
 What I want to know is if     X=Y will give me an X that is
 independent of Y.  I think that is the same question hasAliasing is
 trying to answer.  If so then it's a bug.
 --bb

Here's a really ugly, brute force hack that does what you need: http://dsource.org/projects/dstats/browser/trunk/alloc.d I actually needed it in dstats to support newVoid(), which is a speed hack to allocate arrays w/o initializing the contents, to tell the GC whether to scan for pointers. Actually, on looking at it again, it should probably be changed to make function pointers and delegates return false. Not sure if this is what you were looking for. By semantically a pure value type, do you mean that immutable reference data should or sould not be included?
Jan 27 2009
parent dsimcha <dsimcha yahoo.com> writes:
== Quote from dsimcha (dsimcha yahoo.com)'s article
 == Quote from Bill Baxter (wbaxter gmail.com)'s article
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?
 I found "hasAliasing" in D2 which is close, but it seems to report
 true even for static arrays.
 struct X { float[3] vals; }
 pragma(msg, hasAliasing!(X)?"true":"false"); --> true
 Maybe that's just a bug in hasAliasing?
 What I want to know is if     X=Y will give me an X that is
 independent of Y.  I think that is the same question hasAliasing is
 trying to answer.  If so then it's a bug.
 --bb

http://dsource.org/projects/dstats/browser/trunk/alloc.d I actually needed it in dstats to support newVoid(), which is a speed hack to allocate arrays w/o initializing the contents, to tell the GC whether to scan for pointers. Actually, on looking at it again, it should probably be changed to make function pointers and delegates return false. Not sure if this is what you were looking for. By semantically a pure value type, do you mean that immutable reference data should or sould not be included?

Forgot to mention: It's isReferenceType, about 1/3 of the way down.
Jan 27 2009
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Jan 28, 2009 at 11:09 AM, dsimcha <dsimcha yahoo.com> wrote:
 == Quote from Bill Baxter (wbaxter gmail.com)'s article
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?
 I found "hasAliasing" in D2 which is close, but it seems to report
 true even for static arrays.
 struct X { float[3] vals; }
 pragma(msg, hasAliasing!(X)?"true":"false"); --> true
 Maybe that's just a bug in hasAliasing?
 What I want to know is if     X=Y will give me an X that is
 independent of Y.  I think that is the same question hasAliasing is
 trying to answer.  If so then it's a bug.
 --bb

Here's a really ugly, brute force hack that does what you need: http://dsource.org/projects/dstats/browser/trunk/alloc.d I actually needed it in dstats to support newVoid(), which is a speed hack to allocate arrays w/o initializing the contents, to tell the GC whether to scan for pointers. Actually, on looking at it again, it should probably be changed to make function pointers and delegates return false. Not sure if this is what you were looking for. By semantically a pure value type, do you mean that immutable reference data should or sould not be included?

D1, so it doesn't matter, but if it were D2 that would be ok. What I want to know is if I do A=B will changes to A possibly affect B or not. std.traits.hasAliasing seems to do the trick also with the addition of a small isStaticArray!(T) check. There's a port of it I did in std2. Anyway, here it is for the record: /// Part of IsValueType implementation private template HasRawPointerImpl(T...) { static if (T.length == 0) { const result = false; } else { static if (is(T[0] U : U*)) const hasRawAliasing = true; else static if (is(T[0] U : U[])) const hasRawAliasing = !stdTraits.isStaticArray!(T[0]); else const hasRawAliasing = false; const result = hasRawAliasing || HasRawPointerImpl!(T[1 .. $]).result; } } /// Part of the IsValueType implementation private template HasRawAliasing(T...) { const HasRawAliasing = HasRawPointerImpl!(stdTraits.RepresentationTypeTuple!(T)).result; } /// Part of the IsValueType implementation private template HasAliasing(T...) { const HasAliasing = HasRawAliasing!(T) || stdTraits.hasObjects!(T); } /// The IsValueType template private template IsValueType(T...) { const IsValueType = !HasAliasing!(T); } I suppose technically D will allow you to set the static array .ptr to point to something else. But I'm just not going to do that. :-) --bb --bb
Jan 27 2009
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Jan 28, 2009 at 11:11 AM, dsimcha <dsimcha yahoo.com> wrote:
 == Quote from dsimcha (dsimcha yahoo.com)'s article
 == Quote from Bill Baxter (wbaxter gmail.com)'s article
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?
 I found "hasAliasing" in D2 which is close, but it seems to report
 true even for static arrays.
 struct X { float[3] vals; }
 pragma(msg, hasAliasing!(X)?"true":"false"); --> true
 Maybe that's just a bug in hasAliasing?
 What I want to know is if     X=Y will give me an X that is
 independent of Y.  I think that is the same question hasAliasing is
 trying to answer.  If so then it's a bug.
 --bb

http://dsource.org/projects/dstats/browser/trunk/alloc.d I actually needed it in dstats to support newVoid(), which is a speed hack to allocate arrays w/o initializing the contents, to tell the GC whether to scan for pointers. Actually, on looking at it again, it should probably be changed to make function pointers and delegates return false. Not sure if this is what you were looking for. By semantically a pure value type, do you mean that immutable reference data should or sould not be included?

Forgot to mention: It's isReferenceType, about 1/3 of the way down.

Got it. I see looking there that my one line fix to hasAliasing above won't work. It ignores the possibility that the T in a static array T[3] itself may have references. --bb
Jan 27 2009
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Bill Baxter Wrote:
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?

I think IsReferenceType in the templates module of my dlibs may be fit: http://www.fantascienza.net/leonardo/so/dlibs/templates.html Code: http://www.fantascienza.net/leonardo/so/libs_d.zip (Works with D1, Phobos). Bye, bearophile
Jan 27 2009
prev sibling next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Wed, Jan 28, 2009 at 11:26 AM, bearophile <bearophileHUGS lycos.com> wrote:
 Bill Baxter Wrote:
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?

I think IsReferenceType in the templates module of my dlibs may be fit: http://www.fantascienza.net/leonardo/so/dlibs/templates.html Code: http://www.fantascienza.net/leonardo/so/libs_d.zip (Works with D1, Phobos).

Yep -- dimscha's function is annotated "// thanks bearophile". So I think it's probably the same thing. Thanks to both of you. :-) --bb
Jan 27 2009
prev sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Bill Baxter wrote:
 Does anyone have a good template for checking if a struct is
 semantically a pure value type or not?
 I found "hasAliasing" in D2 which is close, but it seems to report
 true even for static arrays.
 
 struct X { float[3] vals; }
 pragma(msg, hasAliasing!(X)?"true":"false"); --> true
 
 Maybe that's just a bug in hasAliasing?
 
 What I want to know is if     X=Y will give me an X that is
 independent of Y.  I think that is the same question hasAliasing is
 trying to answer.  If so then it's a bug.
 
 --bb

Yah, that's a bug. Could you please bugzillize it? I'm spent. Andrei
Jan 27 2009