Pass class reference to function?

2,600 views
Skip to first unread message

John Chipps-Harding

unread,
Oct 30, 2012, 8:33:16 AM10/30/12
to mi...@dartlang.org
Hi:

Is it possible to pass a reference to a class? E.g.:

Object instanciateThisClass(Class clazz) {
 
return new clazz();
}

I have searched but cannot seem to find a solution.

Any help greatly appreciated!

Regards:


John

Srinivas JONNALAGADDA

unread,
Oct 30, 2012, 9:43:05 AM10/30/12
to mi...@dartlang.org
On Tuesday, 30 October 2012 18:03:16 UTC+5:30, John Chipps-Harding wrote:
Is it possible to pass a reference to a class? E.g.:

Object instanciateThisClass(Class clazz) {
 
return new clazz();
}

I have searched but cannot seem to find a solution.
 
        You can try the following John.

import 'dart:mirrors' as Mirrors;

class A {
  int i = 0;

  A(this.i);
}

Future foo(ClassMirror classMirror) {
  var newInstance = null;

  var future = classMirror.newInstance('', [42]);
  future.then((val) {
    newInstance = val.reflectee;
    print(newInstance);
    print(newInstance.runtimeType);
    print(newInstance.i);
  });

  return future;
}

main() {
  var obj = new A(4);
  var instanceMirror = Mirrors.reflect(obj);
  var classMirror = instanceMirror.type;

  var future = foo(classMirror);
  Futures.wait([future]);
}

- |0|0|
____

Vyacheslav Egorov

unread,
Oct 30, 2012, 11:12:17 AM10/30/12
to mi...@dartlang.org
Lightweight solution would be to just use closures instead of mirrors:

instantiate(f) {
return f();
}

instantiate(() => new A());
instantiate(() => new B());


--
Vyacheslav Egorov
> --
> Consider asking HOWTO questions at Stack Overflow:
> http://stackoverflow.com/tags/dart
>
>

Gilad Bracha

unread,
Oct 30, 2012, 1:45:30 PM10/30/12
to General Dart Discussion
The latter is your best option. Pretty soon, you will be able to pass classes as objects, but you still won't be able to call new on them.  Using mirrors is possible, but much too heavyweight.  Also, at the moment, they only work on the VM. When they do work in dart2js, you will need to exercise judgement because they may increase your download size substantially (depending what you do and what your alternatives are).
--
Cheers, Gilad

Srinivas JONNALAGADDA

unread,
Oct 31, 2012, 3:18:54 AM10/31/12
to mi...@dartlang.org
On Tuesday, 30 October 2012 23:15:34 UTC+5:30, Gilad Bracha wrote:
The latter is your best option. Pretty soon, you will be able to pass classes as objects, but you still won't be able to call new on them.  Using mirrors is possible, but much too heavyweight.  Also, at the moment, they only work on the VM. When they do work in dart2js, you will need to exercise judgement because they may increase your download size substantially (depending what you do and what your alternatives are).

         Agreed, Gilad!  By the way, what operations shall be permitted on class objects?  Construction is ruled out based on what you have said.  Static method invocation?  What else?  Thanks.

Lasse R.H. Nielsen

unread,
Oct 31, 2012, 6:06:52 AM10/31/12
to mi...@dartlang.org
The current Type objects have no methods except what they inherit from Object. Of those, only toString is special in that it gives the name of the type.
All you can use them for is to get that name, and to use as an input to the mirror system if you want to mirror a class.

/L
-- 
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Reply all
Reply to author
Forward
0 new messages