Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 28, 2020 Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
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) |
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ruby The Roobster | 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
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to k2aj | 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.
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ruby The Roobster | 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.
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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)
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ruby The Roobster | 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.
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | 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.
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ruby The Roobster | 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
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ruby The Roobster | 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));
}
```
|
September 28, 2020 Re: Any way to tell if an object is inside another class? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | 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?
|
Copyright © 1999-2021 by the D Language Foundation