Possible to get the name of Type (runtimeType)?

3,539 views
Skip to first unread message

Tom Yeh

unread,
Oct 27, 2012, 12:14:19 PM10/27/12
to mi...@dartlang.org
The name of a class is retrievable from mirrors, but it is too heavy for just retrieving the class's name. It will be great if Type has a property providing the class name.

Justin Fagnani

unread,
Oct 27, 2012, 1:12:09 PM10/27/12
to General Dart Discussion
That information may not be there at runtime, due to minification, which is why you have to use mirrors to get it. It's similar to GWT where Class.getName() only gives you a useful answer if you chose to preserved that metadata when compiling, which increases the size of your app.

Dart doesn't have to implement an API not designed for a optimized JS runtime environment like GWT did, so rather than have a half functioning API, Type only provides what is guaranteed to be available in all runtimes, which is more of an opaque handle.

-Justin



On Sat, Oct 27, 2012 at 9:14 AM, Tom Yeh <tom....@gmail.com> wrote:
The name of a class is retrievable from mirrors, but it is too heavy for just retrieving the class's name. It will be great if Type has a property providing the class name.

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

Lasse R.H. Nielsen

unread,
Oct 27, 2012, 2:09:39 PM10/27/12
to mi...@dartlang.org
On Sat, Oct 27, 2012 at 6:14 PM, Tom Yeh <tom....@gmail.com> wrote:
The name of a class is retrievable from mirrors, but it is too heavy for just retrieving the class's name. It will be great if Type has a property providing the class name.

Try using the toString on the Type object. It should give you a name for the type.
It should be the same name that occurs in Object.toString's "[Instance of Foo]".

No guarantees that it always works, though :)
/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

Justin Fagnani

unread,
Oct 27, 2012, 2:24:40 PM10/27/12
to General Dart Discussion
On Sat, Oct 27, 2012 at 11:09 AM, Lasse R.H. Nielsen <l...@google.com> wrote:


On Sat, Oct 27, 2012 at 6:14 PM, Tom Yeh <tom....@gmail.com> wrote:
The name of a class is retrievable from mirrors, but it is too heavy for just retrieving the class's name. It will be great if Type has a property providing the class name.

Try using the toString on the Type object. It should give you a name for the type.
It should be the same name that occurs in Object.toString's "[Instance of Foo]".

No guarantees that it always works, though :)

Yeah, at some point soon, that will quite possibly break in dart2js.
 

/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

John Evans

unread,
Oct 27, 2012, 3:08:48 PM10/27/12
to mi...@dartlang.org
Is there a dart2js Flag to suppress tree shaking?

Daniel Rodríguez

unread,
Oct 27, 2012, 6:20:42 PM10/27/12
to mi...@dartlang.org
As far as I know, no. It is impossible to compile to JS without tree shaking. It is an integral part of the compilation process, and to make it optional would require to create yet another dart to JS compiler. Don't take this as an official statement, but I am highly confident since I learned this from Gilad himself.

Kasper Lund

unread,
Oct 29, 2012, 4:03:34 AM10/29/12
to General Dart Discussion
On Sun, Oct 28, 2012 at 12:20 AM, Daniel Rodríguez
<seth.i...@gmail.com> wrote:
> As far as I know, no. It is impossible to compile to JS without tree
> shaking. It is an integral part of the compilation process, and to make it
> optional would require to create yet another dart to JS compiler. Don't take
> this as an official statement, but I am highly confident since I learned
> this from Gilad himself.

It would be very easy for us to add a flag that disables tree shaking
in dart2js -- all we have to do is add all methods we see to our
compilation worklist and treat all classes as possibly instantiated.
We do not have such a flag yet.

Cheers,
Kasper

Tom Yeh

unread,
Oct 29, 2012, 4:28:19 AM10/29/12
to mi...@dartlang.org
Can it be part of tree shaking? If Type.name is referenced in app, it is preserved. Otherwise, it is removed just like normal tree shaking.

Justin Fagnani

unread,
Oct 29, 2012, 12:58:46 PM10/29/12
to General Dart Discussion
That could work when you're just statically referencing things like String.name, but that doesn't seem too useful. More likely you're storing types in variables and collections and have functions that take types and then call .name(). Figuring out which type names to preserve would be tough.

Ladislav Thon

unread,
Oct 29, 2012, 1:26:19 PM10/29/12
to mi...@dartlang.org


> That could work when you're just statically referencing things like String.name, but that doesn't seem too useful. More likely you're storing types in variables and collections and have functions that take types and then call .name(). Figuring out which type names to preserve would be tough.

How are GWT people doing this? They AFAIK support Class.getName().

I'd say that type name is important and my guess would be that it's also relatively cheap. What about just remembering it in _all_ cases?

LT

Justin Fagnani

unread,
Oct 29, 2012, 1:41:34 PM10/29/12
to General Dart Discussion
On Mon, Oct 29, 2012 at 10:26 AM, Ladislav Thon <lad...@gmail.com> wrote:


> That could work when you're just statically referencing things like String.name, but that doesn't seem too useful. More likely you're storing types in variables and collections and have functions that take types and then call .name(). Figuring out which type names to preserve would be tough.

How are GWT people doing this? They AFAIK support Class.getName().

Just checked, and I was remembering this backwards, you have to explicitly disable class metadata, otherwise Class.getName() does work. GWT will try to keep metadata only for classes that it knows have getName() called on them, but in many cases it can't figure this out and does keep it for all classes, which is what prompted the option to disable all metadata.


They don't really "disable" the metadata, but obfuscate it. So Class.getName() returns a value, it's just not human readable.

I'd say that type name is important and my guess would be that it's also relatively cheap. What about just remembering it in _all_ cases?

There can be a lot of classes and therefore a lot of unused strings sitting around in an app...

Cheers,
  Justin

 

LT

Tom Yeh

unread,
Oct 31, 2012, 9:16:23 PM10/31/12
to mi...@dartlang.org


There can be a lot of classes and therefore a lot of unused strings sitting around in an app...

It matters only if there are a lot of tiny classes. Otherwise, the names will take only a small part of memory.
A comprise is to use annotation to tell dart2js to preserve the class names for the given classes.
Reply all
Reply to author
Forward
0 new messages