Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

di: Provider with useValue doesn't accept const complex data type instances #10

Closed
frankpepermans opened this issue Jul 20, 2016 · 8 comments
Milestone

Comments

@frankpepermans
Copy link

In beta 18, declaring a Provider with useValue currently fails in the angular transfomer if you use an instance of a complex data type.

For example, use the following setup to define a component:

class Bar {
   const Bar();
}

@Component(
    selector: 'foo',
    template: '<div></div>',
    providers: const [const Provider(DisplayOptions, useValue: const Bar())]
)
class Foo {
}

This throws Invalid argument(s): Incorrect identifier "const Bar()"

And the stack trace is:

package:angular2/src/transform/common/type_metadata_reader.dart 1357:7   _readIdentifier
package:angular2/src/transform/common/type_metadata_reader.dart 1375:12  _readValue
package:angular2/src/transform/common/type_metadata_reader.dart 1166:20  _readProvider.<fn>
@frankpepermans
Copy link
Author

Current workaround:

use useFactory instead of useValue, and simply return the const instance in the factory method body

@matanlurey
Copy link
Contributor

Could you try this:

const displayOptions = const Bar();

@Component(
    selector: 'foo',
    template: '<div></div>',
    providers: const [const Provider(DisplayOptions, useValue: displayOptions)]
)

It might be slightly less hacky. The current Angular analyzer/compiler is pretty rudimentary, but we have plans to improve it to include full package resolution. Can update on this soon.

@matanlurey
Copy link
Contributor

P3 since there is a workaround.

Blocked by #48

@matanlurey
Copy link
Contributor

Just an update: this is still blocked by #48. We have some early work done though towards getting the type information we need to actually resolve the value.

@matanlurey matanlurey added this to the V4 Beta milestone Apr 4, 2017
@matanlurey
Copy link
Contributor

This is now planned for the 4.0 release.

@matanlurey
Copy link
Contributor

This is now unblocked, with caveats:

  • useFactory is still required when the resolved instance uses a private constructor:
class Foo {
  const Foo();
}

const Provider(Foo, useValue: const Foo()) // OK

class Bar {
  static const instance = const Bar._();

  const Bar._();
}

const Provider(Bar, useValue: Bar.instance) // NOT OK, we only see "Bar._".

This isn't something we can solve in the short-term, but it will make providers more useful for public constructors. I'll give an update when folks are able to start using this externally (initially behind a flag, and hopefully the default for 4.0.0).

@matanlurey
Copy link
Contributor

matanlurey commented May 24, 2017

Blocked by #404, which is blocked by #390.

@matanlurey
Copy link
Contributor

Closing since it technically has landed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants