digitalmars.D.learn - how to test if member of instance exists/defined?
- StarGrazer (2/2) Mar 22 2017 I've tried compiles but I guess that only checks if the code has
- crimaniak (2/4) Mar 22 2017 https://dlang.org/spec/traits.html#hasMember
- StarGrazer (8/12) Mar 22 2017 I tried that but D complains
- StarGrazer (4/16) Mar 22 2017 It's not that it doesn't work but the code still trying to be
- StarGrazer (2/23) Mar 22 2017 nevermind, I was being stupid! ;)
- H. S. Teoh via Digitalmars-d-learn (33/35) Mar 22 2017 I'm not sure what you mean by "member of instance", but if you mean
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 2017
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 2017
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 tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.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 2017
On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote:On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote: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.On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.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 2017
On Wednesday, 22 March 2017 at 21:04:48 UTC, StarGrazer wrote:On Wednesday, 22 March 2017 at 21:02:41 UTC, StarGrazer wrote:nevermind, I was being stupid! ;)On Wednesday, 22 March 2017 at 20:53:17 UTC, crimaniak wrote: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.On Wednesday, 22 March 2017 at 20:35:27 UTC, StarGrazer wrote:I tried that but D complains static if (hasMember!(S, "x")) return; else s.x = 3; D says that x doesn't exist.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 2017
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 2017