Skip to content

If I only use registerLazySingleton, is there an advantage over a static instance? #88

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

Closed
creativecreatorormaybenot opened this issue Jun 16, 2020 · 7 comments

Comments

@creativecreatorormaybenot

Taking a look at the Flutter framework, you will notice the following pattern for singletons:

https://github.com/flutter/flutter/blob/4d7525f05c05a6df0b29396bc9eb78c3bf1e9f89/packages/flutter/lib/src/services/raw_keyboard.dart#L493

  /// The shared instance of [RawKeyboard].
  static final RawKeyboard instance = RawKeyboard._();

I think that this is functionally equivalent to get_it's registerLazySingleton as final variables that are "top-level or class variable[s are] initialized the first time [they are] used".

Am I correct with this assumption?

In that case, which is at least the most common use-case for me, I would not need get_it. Then, only the additional features are appealing (might be worth a note in the README then) 🙃

@escamoteur
Copy link
Collaborator

Please see #87

What you are describing isn't lazy but will be instantiated immediately on App startup as its an static variable.
also the idea of the singleton pattern is that is should ensure that there really is only one instance in your app.

I would especially not the async support of GetIt and that you easily switch implementations

@creativecreatorormaybenot
Copy link
Author

creativecreatorormaybenot commented Jun 23, 2020

@escamoteur Are you sure about the instantiation part? I am pretty sure that this is not the case (see article and discussion).

@escamoteur
Copy link
Collaborator

Interesting, I'm not sure if this was implemented as discussed, but I will try to find out.

@escamoteur
Copy link
Collaborator

I just verified, you are correct. although I couldn't find anything in the linked article, but maybe I oversaw something.
Anyway, all global variables and therefore static ones too are initialized lazy.
Thanks to point me to this.

@creativecreatorormaybenot
Copy link
Author

@escamoteur Btw, the same applies to top-level final variables because they are also lazily initialized.

So, the following is true:

final topLevelSingleton = Singleton();
// The above is a shorthand for:

void main() {
  GetIt.instance.registerLazySingleton(() => Singleton());
}

As I said before, there are obviously advantages that GetIt can have - just in this basic usage it performs worse afaic.

@escamoteur
Copy link
Collaborator

Yeah only that you have then many global variables and you can not switch implementations.

@creativecreatorormaybenot
Copy link
Author

@escamoteur Exactly.

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

No branches or pull requests

2 participants