www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Any way to tell if an object is inside another class?

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
For example:

class test {}
class T {
auto c = new test();
}

Any way to tell if an object of type test is a member of object 
T? I don't want to use the name of the member variable. I just 
want to know if this works in general.
Why am I asking this? Because I need it to develop this Multiple 
Alias This project I am working on(basically just mashing all the 
functions into a class and then using the class with alias this)
Sep 28 2020
next sibling parent reply k2aj <krzysztof.jajesnica gmail.com> writes:
On Monday, 28 September 2020 at 11:11:13 UTC, Ruby The Roobster 
wrote:
 For example:

 class test {}
 class T {
 auto c = new test();
 }

 Any way to tell if an object of type test is a member of object 
 T? I don't want to use the name of the member variable. I just 
 want to know if this works in general.
 Why am I asking this? Because I need it to develop this 
 Multiple Alias This project I am working on(basically just 
 mashing all the functions into a class and then using the class 
 with alias this)
You can use FieldTypeTuple!T to get a compile time sequence of T's field types, then use anySatisfy to check whether the field types contain the type you want: import std.traits : FieldTypeTuple; import std.meta : anySatisfy; template typeEquals(T) { enum typeEquals(U) = is(T == U); } enum hasFieldOfType(Obj, Type) = anySatisfy!( typeEquals!Type, FieldTypeTuple!Obj ); pragma(msg, hasFieldOfType!(T, int)); //false pragma(msg, hasFieldOfType!(T, test)); //true
Sep 28 2020
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Monday, 28 September 2020 at 11:40:40 UTC, k2aj wrote:
 pragma(msg, hasFieldOfType!(T, int));  //false
 pragma(msg, hasFieldOfType!(T, test)); //true
Not exactly what I meant. I more of meant is there a way to check if a test object is a member variable, not if it is in a particular class.
Sep 28 2020
prev sibling next sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 28 September 2020 at 11:11:13 UTC, Ruby The Roobster 
wrote:
 For example:

 class test {}
 class T {
 auto c = new test();
 }

 Any way to tell if an object of type test is a member of object 
 T? I don't want to use the name of the member variable. I just 
 want to know if this works in general.
Can you give some examples of inputs and corresponding outputs for this, like you would for a unit test? I don't understand exactly what you're asking, and it would help clarify.
Sep 28 2020
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Monday, 28 September 2020 at 13:00:43 UTC, Paul Backus wrote:

 Can you give some examples of inputs and corresponding outputs 
 for this, like you would for a unit test? I don't understand 
 exactly what you're asking, and it would help clarify.
Okay. Here is an example. class Test { this.is_in_aggregate } I want a function that returns true when the parent object is a member variable of an aggregate type(UDA)
Sep 28 2020
parent reply Paul Backus <snarwin gmail.com> writes:
On Monday, 28 September 2020 at 13:45:30 UTC, Ruby The Roobster 
wrote:
 On Monday, 28 September 2020 at 13:00:43 UTC, Paul Backus wrote:

 Can you give some examples of inputs and corresponding outputs 
 for this, like you would for a unit test? I don't understand 
 exactly what you're asking, and it would help clarify.
Okay. Here is an example. class Test { this.is_in_aggregate } I want a function that returns true when the parent object is a member variable of an aggregate type(UDA)
Can you re-write this as actual valid D code, but with the implementation of the function stubbed out? I still don't understand what your function is supposed to take as its input(s), or what "parent object is a member variable of an aggregate type" means (as far as I'm aware, objects are *values*, not *variables*), or what UDAs have to do with any of this.
Sep 28 2020
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Monday, 28 September 2020 at 14:09:07 UTC, Paul Backus wrote:
 Can you re-write this as actual valid D code, but with the 
 implementation of the function stubbed out? I still don't 
 understand what your function is supposed to take as its 
 input(s), or what "parent object is a member variable of an 
 aggregate type" means (as far as I'm aware, objects are 
 *values*, not *variables*), or what UDAs have to do with any of 
 this.
I meant User Defined types. not UDAs. Anyways, the whole thing is me trying to find a hacky workaround that allows something similar to multiple alias this declarations(because multiple of these are not possible). And for this, I have to determine if a normal number is being passed, or if an user defined type is being passed through the parameter.
Sep 28 2020
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Monday, 28 September 2020 at 14:22:34 UTC, Ruby The Roobster 
wrote:
 I meant User Defined types. not UDAs. Anyways, the whole thing 
 is me trying to find a hacky workaround that allows something 
 similar to multiple alias this declarations(because multiple of 
 these are not possible). And for this, I have to determine if a 
 normal number is being passed, or if an user defined type is 
 being passed through the parameter.
I mean type, not number
Sep 28 2020
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 28 September 2020 at 14:23:12 UTC, Ruby The Roobster 
wrote:
 On Monday, 28 September 2020 at 14:22:34 UTC, Ruby The Roobster 
 wrote:
 I meant User Defined types. not UDAs. Anyways, the whole thing 
 is me trying to find a hacky workaround that allows something 
 similar to multiple alias this declarations(because multiple 
 of these are not possible). And for this, I have to determine 
 if a normal number is being passed, or if an user defined type 
 is being passed through the parameter.
I mean type, not number
There's the `parent` trait. You can wrap it like this: ``` import std; class Foo { int x; } struct Bar { Foo f; } Foo g; enum hasParent(alias sym) = is(__traits(parent, sym) == class) || is(__traits(parent, sym) == struct); void main() { writeln(hasParent!(Bar.f)); // true writeln(hasParent!(g)); } ```
Sep 28 2020
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Monday, 28 September 2020 at 14:36:01 UTC, Mike Parker wrote:

 There's the `parent` trait. You can wrap it like this:
`` then is (hasParent!f) legal code?
Sep 28 2020
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/28/20 7:11 AM, Ruby The Roobster wrote:
 For example:
 
 class test {}
 class T {
 auto c = new test();
 }
 
 Any way to tell if an object of type test is a member of object T? I 
 don't want to use the name of the member variable. I just want to know 
 if this works in general.
 Why am I asking this? Because I need it to develop this Multiple Alias 
 This project I am working on(basically just mashing all the functions 
 into a class and then using the class with alias this)
No. The type of e.g. T.c is the same as the type of some other instance of `test`. Armed only with a class reference to a `test` object, you cannot tell where it is referenced from. I think you need to be more specific about what you want to do. I have a feeling you are not understanding how classes work in D (they are not stored inside an object or struct, but rather on the heap, and the member variable is simply a reference to that item). -Steve
Sep 28 2020