digitalmars.D - IsValueType template? std.traits.hasAliasing?
- Bill Baxter (11/11) Jan 27 2009 Does anyone have a good template for checking if a struct is
- dsimcha (9/20) Jan 27 2009 Here's a really ugly, brute force hack that does what you need:
- dsimcha (2/22) Jan 27 2009 Forgot to mention: It's isReferenceType, about 1/3 of the way down.
- Bill Baxter (6/28) Jan 27 2009 Got it.
- Bill Baxter (45/65) Jan 27 2009 D1, so it doesn't matter, but if it were D2 that would be ok.
- bearophile (8/10) Jan 27 2009 I think IsReferenceType in the templates module of my dlibs may be fit:
- Bill Baxter (5/13) Jan 27 2009 Yep -- dimscha's function is annotated "// thanks bearophile". So I
- Andrei Alexandrescu (3/18) Jan 27 2009 Yah, that's a bug. Could you please bugzillize it? I'm spent.
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
== Quote from Bill Baxter (wbaxter gmail.com)'s articleDoes 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. --bbHere'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
== Quote from dsimcha (dsimcha yahoo.com)'s article== Quote from Bill Baxter (wbaxter gmail.com)'s articleForgot to mention: It's isReferenceType, about 1/3 of the way down.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. --bbHere'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
On Wed, Jan 28, 2009 at 11:11 AM, dsimcha <dsimcha yahoo.com> wrote:== Quote from dsimcha (dsimcha yahoo.com)'s articleGot 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== Quote from Bill Baxter (wbaxter gmail.com)'s articleForgot to mention: It's isReferenceType, about 1/3 of the way down.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. --bbHere'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
On Wed, Jan 28, 2009 at 11:09 AM, dsimcha <dsimcha yahoo.com> wrote:== Quote from Bill Baxter (wbaxter gmail.com)'s articleD1, 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 --bbDoes 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. --bbHere'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
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
On Wed, Jan 28, 2009 at 11:26 AM, bearophile <bearophileHUGS lycos.com> wrote:Bill Baxter Wrote:Yep -- dimscha's function is annotated "// thanks bearophile". So I think it's probably the same thing. Thanks to both of you. :-) --bbDoes 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).
Jan 27 2009
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. --bbYah, that's a bug. Could you please bugzillize it? I'm spent. Andrei
Jan 27 2009