New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectMirror.invoke ignores typeArguments #33594

Open
thosakwe opened this Issue Jun 23, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@thosakwe
Copy link

thosakwe commented Jun 23, 2018

  • Dart SDK Version: 2.0.0-dev.64.1
  • Mac OSX Sierra
  • Browser: N/A

I'm updating the package json_god to work with Dart 2; it's a dart:mirrors-based reflection solution. The actual functionality works, but for the fact that Dart 2's generics are reified, and so to work with typed collections, I need to somehow attach type information to objects.

The solution I am attempting is to reflectively call List.cast with the necessary type argument; however, the ObjectMirror.delegate method forwards to ObjectMirror.invoke, which doesn't use the type arguments. Thus, the type arguments are ignored, and items like List<String> remain List<dynamic>.


My code:

logger.info('Casting list elements to ${typeArguments[0].reflectedType}');
var inv = new Invocation.genericMethod(#cast,
   [typeArguments[0].reflectedType], []);
logger.info('INVOCATION OF ${inv.memberName} with type args: ${inv
    .typeArguments}');
var output = reflect(it.toList()).delegate(inv);
logger.info('Casted list type: ${output.runtimeType}');

The current output:

[INFO] json_god: Casting list elements to String
[INFO] json_god: INVOCATION OF Symbol("cast") with type args: [String]
[INFO] json_god: About to deserialize es2015 to a String
[INFO] json_god: Value es2015 is a primitive
[INFO] json_god: About to deserialize stage-0 to a String
[INFO] json_god: Value stage-0 is a primitive
[INFO] json_god: Casted list type: CastList<dynamic, dynamic>

It's evident that that the type arguments are not actually being applied at all. I can't think of any possible workaround for this, unfortunately.

@thosakwe thosakwe changed the title ObjectMirror.invoke doesn't support typeArguments Instance.invoke doesn't support typeArguments Jun 23, 2018

@thosakwe thosakwe changed the title Instance.invoke doesn't support typeArguments InstanceMirror.invoke doesn't support typeArguments Jun 23, 2018

@thosakwe thosakwe changed the title InstanceMirror.invoke doesn't support typeArguments ObjectMirror.invoke doesn't support typeArguments Jun 23, 2018

thosakwe added a commit to thosakwe/json_god that referenced this issue Jun 23, 2018

@thosakwe thosakwe changed the title ObjectMirror.invoke doesn't support typeArguments ObjectMirror.invoke ignores typeArguments Jun 23, 2018

@thosakwe

This comment has been minimized.

Copy link

thosakwe commented Jul 6, 2018

Is there anything else I can do about this? This bug means that anything that uses dart:mirrors for serialization, etc., will not be able to perform casts, and always produce runtime errors.

@devoncarew

This comment has been minimized.

Copy link
Member

devoncarew commented Jul 7, 2018

@matanlurey

This comment has been minimized.

Copy link
Contributor

matanlurey commented Jul 7, 2018

FWIW, you really won't want to use .cast as part of a normal workflow. It creates a forwarding implementation of the underlying collection, and is mostly meant to be a temporary stop-gap. For more data sets, a defensive copy is probably better:

https://www.dartlang.org/guides/language/effective-dart/usage#avoid-using-cast

(Still doesn't help you with mirrors, of course)

@sestegra

This comment has been minimized.

Copy link

sestegra commented Jul 8, 2018

Did you try List<T>.from ?

@thosakwe

This comment has been minimized.

Copy link

thosakwe commented Jul 11, 2018

Thanks for the tip - I was able to resolve it by using List.from, which works perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment