3 ms·
I think keypaths also permit writing (but maybe the rvalue-reference does that?). And probably they allow using properties and not just fields, but I don't know
by captainmuon 4y ago
I think keypaths also permit writing (but maybe the rvalue-reference does that?). And probably they allow using properties and not just fields, but I don't know Swift tbh.
But this touches on one thing I think would be interesting in a language. On the one hand you can define a function like you did, which is a black box that does something. On the other hand, you can talk about "the action of doing something", and assign that description to a variable, introspect it, change the action. Maybe convert from imperative to command-pattern, so you can undo, and so on. I think one of the few languages that allows something like this is Lisp. Keypaths are somewhat declarative, but don't allow introspection and are limited to getting/setting it seems.
- gpderetta 4y agoDuring return type inference, references decay to value types by default, so you have to use some awkward syntax if you want to use them to set values: auto item_user = [](auto&& item) -> decltype(auto) { return (item.user); }; item_user(item) = my_user; Even more awkward if you want some slightly better error messages with concepts: auto item_user = [](auto&& item) -> decltype(auto) requires requires { item.user;} { return (item.user); }; (yes, the double requires is not a mistake). At least is very macroable, which would also allow to wrap it in a type with more meaning than a opaque lambda (which I agree is a good thing). edit: Full on nonsense just for laughs: auto user_age = [](auto&& user) noexcept (noexcept((user.age))) -> decltype(auto) requires requires { user.age;} { return (user.age); };