www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to test if member of instance exists/defined?

reply StarGrazer <Stary Night.com> writes:
I've tried compiles but I guess that only checks if the code has 
valid syntax, not if it actually will compile in context.
Mar 22
next sibling parent reply crimaniak <crimaniak gmail.com> writes:
On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:
 I've tried compiles but I guess that only checks if the code 
 has valid syntax, not if it actually will compile in context.
https://dlang.org/spec/traits.html#hasMember
Mar 22
parent reply StarGrazer <Stary Night.com> writes:
On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote:
 On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:
 I've tried compiles but I guess that only checks if the code 
 has valid syntax, not if it actually will compile in context.
https://dlang.org/spec/traits.html#hasMember
I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.
Mar 22
parent reply StarGrazer <Stary Night.com> writes:
On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote:
 On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote:
 On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:
 I've tried compiles but I guess that only checks if the code 
 has valid syntax, not if it actually will compile in context.
https://dlang.org/spec/traits.html#hasMember
I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.
It's not that it doesn't work but the code still trying to be compiled by the compiler, when I don't want it to.
Mar 22
parent StarGrazer <Stary Night.com> writes:
On Wednesday, 22 March 2017 at 21:04:48 UTC, StarGrazer wrote:
 On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote:
 On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote:
 On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:
 I've tried compiles but I guess that only checks if the code 
 has valid syntax, not if it actually will compile in context.
https://dlang.org/spec/traits.html#hasMember
I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.
It's not that it doesn't work but the code still trying to be compiled by the compiler, when I don't want it to.
nevermind, I was being stupid! ;)
Mar 22
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Mar 22, 2017 at 08:35:27PM +0000, StarGrazer via Digitalmars-d-learn
wrote:
 I've tried compiles but I guess that only checks if the code has valid
 syntax, not if it actually will compile in context.
I'm not sure what you mean by "member of instance", but if you mean whether some given type T, presumably an aggregate like a struct, has some member x, here's how to do it: Code: struct StructA { int x; } struct StructB { int y; } template CheckMembers(T) { static if (is(typeof(T.init.x))) pragma(msg, T.stringof ~ " has member named x"); else pragma(msg, T.stringof ~ " doesn't have a member named x"); } alias dummy1 = CheckMembers!StructA; alias dummy2 = CheckMembers!StructB; Compiler output: StructA has member named x StructB doesn't have a member named x The key is to use is(typeof(...)) as the check. The idea being that if the member doesn't exist, then the compiler won't be able to find a type for the member, so it will not have a valid type and is(...) will return false. Whereas if the member does exist, then it will have some valid type (and it doesn't matter what that type is) and is(...) will return true. Generally, using is(typeof(...)) is preferable to using __traits(compiles, ...) where possible. T -- If Java had true garbage collection, most programs would delete themselves upon execution. -- Robert Sewell
Mar 22