www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [D1] type of type

reply %u <e ee.com> writes:
Is it possible to give a function a class(type) as an argument such that the
function can call its constructor, without using templates.

void func(T t){
  new T();
}

Or, what is the type of a type? :)
Dec 23 2010
parent reply %u <e ee.com> writes:
Should have been this:

void func(type t){
  new t();
}
Dec 23 2010
next sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Fri, 24 Dec 2010 01:28:49 +0300, %u <e ee.com> wrote:

 Should have been this:

 void func(type t){
   new t();
 }

Try this (not tested): class Test {} Object o = Object.factory("Test");
Dec 23 2010
parent reply %u <e ee.com> writes:
== Quote from Denis Koroskin (2korden gmail.com)'s article
 On Fri, 24 Dec 2010 01:28:49 +0300, %u <e ee.com> wrote:
 Should have been this:

 void func(type t){
   new t();
 }

class Test {} Object o = Object.factory("Test");

Thanks, Hiding in Object.. interesting :) Making the type a string, does this make using small class names more efficient?
Dec 23 2010
parent bearophile <bearophileHUGS lycos.com> writes:
%u:

 Hiding in Object.. interesting :)

But this has strong limitations. For this problem templates are usually used. Bye, bearophile
Dec 24 2010
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 23 Dec 2010 17:28:49 -0500, %u <e ee.com> wrote:

 Should have been this:

 void func(type t){
   new t();
 }

void func(T)(){ new T(); } When you are passing types into functions, use templates. -Steve
Dec 27 2010
parent reply %u <e ee.com> writes:
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 On Thu, 23 Dec 2010 17:28:49 -0500, %u <e ee.com> wrote:
 Should have been this:

 void func(type t){
   new t();
 }

new T(); } When you are passing types into functions, use templates. -Steve

The reason I asked for a non-templated solution is because they don't have a common interface signature.
Dec 29 2010
next sibling parent %u <e ee.com> writes:
== Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 On Wed, 29 Dec 2010 10:33:21 -0500, %u <e ee.com> wrote:
 == Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 On Thu, 23 Dec 2010 17:28:49 -0500, %u <e ee.com> wrote:
 Should have been this:

 void func(type t){
   new t();
 }

new T(); } When you are passing types into functions, use templates. -Steve

The reason I asked for a non-templated solution is because they don't have a common interface signature.

interface. This works with any type: void func(T)(){ T t; } Maybe you can post an example of what you are trying to solve? -Steve

Yeah, sorry, I meant it the other way around: I need a common interface. class C1: I .. class C9: I I'd like to pass any C(a) type to any C(b) object such that C(b) can spawn a C(a). What would be the common signature of these two functions? And how would the object save the type?
Dec 29 2010
prev sibling parent %u <e ee.com> writes:
Is it not possible to have a "type" type?
Dec 29 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 29 Dec 2010 10:33:21 -0500, %u <e ee.com> wrote:

 == Quote from Steven Schveighoffer (schveiguy yahoo.com)'s article
 On Thu, 23 Dec 2010 17:28:49 -0500, %u <e ee.com> wrote:
 Should have been this:

 void func(type t){
   new t();
 }

new T(); } When you are passing types into functions, use templates. -Steve

The reason I asked for a non-templated solution is because they don't have a common interface signature.

I don't know what you mean. Templated solution does not require a common interface. This works with any type: void func(T)(){ T t; } Maybe you can post an example of what you are trying to solve? -Steve
Dec 29 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Wed, 29 Dec 2010 19:25:05 -0500, %u <e ee.com> wrote:

 Is it not possible to have a "type" type?

In compile time, you can have type parameters with ease using templates. During runtime, you can use the TypeInfo object, which is accessed via typeid: auto ti = typeid(int); // ti is TypeInfo type See docs for TypeInfo in object.di Now, the only issue with TypeInfo is that D runtime reflection is woefully supported. You can't do much with a TypeInfo. So again, what is it you want to do with the type type? -Steve
Dec 30 2010