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

Easy way to define a inline class which inherits all members of the base class. #2854

Closed
Cat-sushi opened this issue Feb 20, 2023 · 4 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@Cat-sushi
Copy link

typedef is a easy way to define a alias of a type, but it doesn't have separate static type to be completed, inferred or checked.
On the other hand, inline class doesn't inherit members of base class.
I want to have easy way to define a inline class which inherits all members of the base class.

For example,

inline class Db = Map<int, String>;
@Cat-sushi Cat-sushi added the feature Proposed language feature that solves one or more problems label Feb 20, 2023
@rubenferreira97
Copy link

rubenferreira97 commented Feb 20, 2023

I really wish something like this was possible. Sometimes I only want to define a superset of operations to a static type. Forwarding the whole constructor is verbose (and in this case I don't really want to create a new class with overhead, just inline it).

Example with an extension:

// Some syntax to combine "inline + extension" would be perfect but I understand if this is too much to ask
inline class MyCustomMap<K,V> = Map<K,V>;

extension on MyCustomMap<K,V> {
   void fancyMethod() => print('Fancy');
}

final Map<int,int> map = {1:1};
map.fancyMethod(); //compile error
final MyCustomMap<int,int> fancyMap = {1,1};
fancyMap.fancyMethod(); //fine

@Cat-sushi
Copy link
Author

Cat-sushi commented Feb 20, 2023

A modifier might be better to override(?) some members of the base class.

inline class LruCache<K, V> {
  inherits Map<K, V> map;
  static int size = 100;

  @override // ?
  V? operator [](K key) {
    var value = map.remove(key);
    if(value != null) {
      map[key] = value;
    }
    return value;
  }

  @override // ?
  void operator []=(K key, V value) {
    map.remove(key);
    map[key] = value;
    if(map.length > size) {
      map.remove(map.keys.first);
    }
  }
}

@eernstg
Copy link
Member

eernstg commented Feb 21, 2023

Cf. #2506.

@Cat-sushi
Copy link
Author

@eernstg Thank you for your information.
#2506 satisfies my wants.
Closing this issue as duplicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

3 participants