rollbear/strong_type

strong_type modifier optional

ksergey opened this issue · 7 comments

Firstly thanks for nice library.

Will be good to see optional modifier for a type which will looks like:

using Qty = strong::type<std::size_t, struct Tag_Qty, strong::optional<0>>;

auto qty = Qty(0);
assert(qty.has_value() == false);

qty = Qty(10);
assert(qty.has_value());

in this case 0 mean no value.

Thanks

I am a little confused here. What is it you would gain over just using std::optional<my_strong_type>?

https://godbolt.org/z/as3jjf1va

Ah, OK, now I think I see. You want to mark a certain value, 0 in your example, as "not having a value", am I right?

I know this is a frequently used pattern in some domains, but I really don't like it much. It's also more complicated, because in many cases (see many posix functions) all negative values means "no value", so it's tricky to define.

You want to mark a certain value, 0 in your example, as "not having a value", am I right?

Yes!

I using SBE (Simple Binary Encoding) for messaging between my components. SBE schema could define null value for a type which indicates no value for a field.

Hmmm. I don't think I will add that to the library. However, I will add a section in the documentation about how to write your own type modifiers, and I will probably use that as an example, because it's a simple one to explain.

Nice, thank you

Here's the docs. It uses functionality not yet on main (for the tests, not the implementation).

https://github.com/rollbear/strong_type/tree/v9-dev#writing_modifier

Super! Thanks!