Skip to content
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

[Feature Request] Generic Types for Box #45

Closed
Afsar-Pasha opened this issue Sep 11, 2019 · 10 comments
Closed

[Feature Request] Generic Types for Box #45

Afsar-Pasha opened this issue Sep 11, 2019 · 10 comments
Labels
enhancement New feature or request

Comments

@Afsar-Pasha
Copy link
Contributor

It would be great if Box support generic types such as Box<K, V>.

@Afsar-Pasha Afsar-Pasha added the enhancement New feature or request label Sep 11, 2019
@simc
Copy link
Member

simc commented Sep 11, 2019

What exactly do you mean? You can store any object in a box.

@Afsar-Pasha
Copy link
Contributor Author

Ex, if I want a box to store only String keys then, by using Hive.openBox<dynamc, String>('box') compiler would allow only to write String as value in Box.put, Box.putAt, etc.
This could reduce bugs, and also compiler can understand the return type of Box.get(key).

@simc
Copy link
Member

simc commented Sep 11, 2019

This only works with values because the add() method implicitly stores an integer key.
So if you would open a box which only supports String keys and you use the add() method, there will be an exception.
I'm not sure if the users expect that...

@Afsar-Pasha
Copy link
Contributor Author

Afsar-Pasha commented Sep 11, 2019

Maybe Box<T> in which value is T and key is always dynamic?

@simc
Copy link
Member

simc commented Sep 11, 2019

I'll implement that 👍

@simc
Copy link
Member

simc commented Sep 11, 2019

I thought about this and (afaik) it is impossible to implement this. The best example are lists: You cannot cast List<dynamic> to List<SomeObject> so if you open a box of type Box<List<SomeObject>>, you expect to get a List<SomeObject> if you call box.get('myKey') but you actually get a List<dynamic>.

If you have a solution for this problem, I would happily implement this feature.

@Afsar-Pasha
Copy link
Contributor Author

I think it can be implemented by using this approach but that would require a little bit of boilerplate and that would be hacky, so closing this issue. Would re-open if I got any neat solution.

@Afsar-Pasha
Copy link
Contributor Author

Afsar-Pasha commented Sep 12, 2019

Instead of that approach we can add an anonymous function to convert List and Map such as

Hive.openBox<List<int>>('box', castToList: (l)=>l.cast<int>().toList())

in BoxImpl

T Function(List<dynamic> l) castToList;
T Function(Map<dynamic, dynamic> m) castToMap;
BoxImpl(... , {this.castToList, this.castToMap} ...) //constructor
...
  T get(dynamic key, {T defaultValue}){
  ...
    if(frame.value is List)
      return castToList(frame.value);
    if(frame.value is Map)
      return castToMap(frame.value);

but still requires some boilerplate and a little bit confusing.

@simc
Copy link
Member

simc commented Sep 12, 2019

Unfortunately cast() only works for "one dimension". List<List<dynamic>>.cast<List<List<int>>() will fail.

I think this is a major flaw of the language :/

@Afsar-Pasha
Copy link
Contributor Author

I mean they can provide a function to convert List<dynamic> to List<T> for that, the function can be

(list) => (l.map((l) => l.cast<int>().toList()).toList().cast<List<int>>().toList())

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

No branches or pull requests

2 participants