[libc-commits] [libc] [libc] Add monadic functions to cpp::expected (PR #66523)

via libc-commits libc-commits at lists.llvm.org
Mon Sep 18 14:54:00 PDT 2023


michaelrj-google wrote:

I like the idea of having a more modular number parser, but I'm not sure that this monadic pattern is the best option.

In the example you provided, it doesn't seem like there would be any difference between calling `and_then` on the next function and just calling that function on its own. None of the intermediate functions (e.g. `consume_optional_sign`) can fail, and they also don't return anything, so they'll always advance to the next one. Additionally I like the idea of limiting which functions interact with which pieces of state, but the final number parsing function always needs to interact with all of them and that seems to defeat the purpose.

What I do like a lot is the idea of templating the reading mechanism, and having a way for it to propagate errors. The current reading mechanism in scanf's int converter is very ugly to handle this, see https://github.com/llvm/llvm-project/blob/main/libc/src/stdio/scanf_core/int_converter.cpp#L96 . The problem is that each read first needs to check if the max width has been hit, leading to several repetitions of that "if (max_width > 1) then read a char, else return 0" pattern. By adding another templated reader layer we could use the same code for scanf and the simple strtol that doesn't need any error checking on read at all. 

https://github.com/llvm/llvm-project/pull/66523


More information about the libc-commits mailing list