Re: Reflection: Get all inherited classes of a base class

2012-12-25 Thread nrgyzer
On Saturday, 22 December 2012 at 22:28:57 UTC, Adam D. Ruppe 
wrote:

On Saturday, 22 December 2012 at 22:14:28 UTC, nrgyzer wrote:
is it possible to get a list of all inherited classes from a 
base class like this:


Yes, though it isn't compile time - gotta be runtime.

ClassInfo[] getChildClasses(ClassInfo c) {
ClassInfo[] info;

// MoudleInfo is a magical thing in object.d,
// implicitly imported, that can loop over all
// modules in the program: user and library
foreach(mod; ModuleInfo) {
// the localClasses member gives back
// ClassInfo things that we can compare
foreach(cla; mod.localClasses) {
// note: we could also check this
// recursively and check
// cla.interfaces as well as base
if(cla.base is c)
info ~= cla;
}
}

return info;
}


So this should be enough for your example assert, but remember 
you can't do things like templates on this, since it is all 
runtime.


Thanks Adam, that's exactly what I need... is it possible to call 
a static method only using TypeInfo_Class or do I need to call 
the constructor using create-method? I think using annotation 
would be very helpful for my idea:


abstract class A {
   static abstract string myName(); // this surely doesn't work
   string[] getChildClassNames() {
  string[] retArray;
  foreach(mod; ModuleInfo) {
 foreach(cla; mod.localClasses) {
if (cla is this.classinfo)
   retArray ~= cla.myName; // adds "Class B" and 
"Class C"

 }
  return retArray;
   }
}

class B : A {
   override static string myName() { return "Class B"; }
}

class C : A {
   override static string myName() { return "Class C"; }
}

But as in the most other languages it's impossible to declare 
abstract static methods and I also have no direct access from 
TypeInfo_Class to my static attributes and/or methods (or is 
there any chance without creating an instance of the class?).


Hence, annotations would be very useful at this point, but is 
there any other possibility to do something like shown above...?


Re: private with alias this

2012-12-25 Thread Zhenya

On Tuesday, 25 December 2012 at 07:21:12 UTC, evilrat wrote:

On Tuesday, 25 December 2012 at 05:28:54 UTC, Zhenya wrote:

On Tuesday, 25 December 2012 at 02:43:52 UTC, r_m_r wrote:

On 12/25/2012 07:42 AM, r_m_r wrote:

assert(!__traits(compiles, b._bar));


sorry, i made a typo: it should be bar_ instead of _bar.

Interestingly, the below assertion fails at run time:

assert(!__traits(compiles, b.bar_));


but this won't compile (as expected):

assert(b.bar_ == 20); //main.d(15): Error: undefined 
identifier 'bar_'



regards,
r_m_r


So,should I file a new bug?Or it's alright and I don't 
understand something?


i don't think it's a bug, because you are just setting alias 
for a member. if you remove private from field it would work. 
i'm just wonder why do you want do something like this?


After your explaination I understood that it's really 
meaningless.Thank you)


is(T == const) for function types

2012-12-25 Thread mist

http://dpaste.dzfl.pl/0cda8d0f

bug or feature?


Re: is(T == const) for function types

2012-12-25 Thread Ali Çehreli

On 12/25/2012 12:59 PM, mist wrote:

http://dpaste.dzfl.pl/0cda8d0f

bug or feature?


For convenience to others, here is your code:

struct Test
{
void delegate() const deleg;
}

void main()
{
static assert( is(typeof(Test.deleg) == const) );
}

I don't know the answer but this works:

struct Test
{
alias void delegate() Deleg;
const Deleg deleg;
}

void main()
{
static assert( is(typeof(Test.deleg) == const) );
}

Ali


Re: is(T == const) for function types

2012-12-25 Thread bearophile

Ali Çehreli:


I don't know the answer but this works:


That difference smells of compiler bug :-)

Bye,
bearophile


Re: is(T == const) for function types

2012-12-25 Thread Ali Çehreli

On 12/25/2012 04:13 PM, bearophile wrote:

Ali Çehreli:


I don't know the answer but this works:


That difference smells of compiler bug :-)

Bye,
bearophile


Hmmm. I think the compiler is right. That const that is applied "at the 
end" in that syntax is I think allowed only for member functions. 
Otherwise these two work as well:


// These work:
const(void delegate()) deleg;
const void delegate() deleg;

// This is a compilation error:
void delegate() const deleg;


Ali


Re: is(T == const) for function types

2012-12-25 Thread bearophile

Ali Çehreli:

Hmmm. I think the compiler is right. That const that is applied 
"at the end" in that syntax is I think allowed only for member 
functions. Otherwise these two work as well:


You seem right. The positional syntax of those tags tricks me 
sometimes still.


Bye,
bearophile