PackRuble / cardoteka

The best type-safe wrapper over SharedPreferences. ⭐ Why so? -> strongly typed cards for access to storage -> don't think about type, use get|set -> can work with nullable values -> callback based updates
https://t.me/+AkGV73kZi_Q1YTMy
Apache License 2.0
2 stars 0 forks source link

Optimization of `_setValue` function operation #7

Open PackRuble opened 7 months ago

PackRuble commented 7 months ago

There's a section of code like this:

switch (card.type) {
  case DataType.bool:
    return _prefs.setBool(key, resultValue as bool);
  case DataType.int:
    return _prefs.setInt(key, resultValue as int);
  case DataType.double:
    return _prefs.setDouble(key, resultValue as double);
  case DataType.string:
    return _prefs.setString(key, resultValue as String);
  case DataType.stringList:
    return _prefs.setStringList(key, (resultValue as List).cast<String>());
}

I like its semantics, it also works great. However, a bad idea popped into my head to optimize this....

What if you do something like this:

<DataType, Object? Function()>{
  DataType.bool: () async => _prefs.setBool(key, resultValue as bool),
  DataType.int: () async => _prefs.setInt(key, resultValue as int),
  //...
};

This already looks nasty semantically + adds an unnecessary lambda function + an unnecessary ! sign when used. But is there any optimization here?