4 ms·
I am still unclear on what mustUse is or does is there any sample code?
by giancarlostoro 4y ago
I am still unclear on what mustUse is or does is there any sample code?
- Doxin 4y agoIt'll throw an error for myFunc(); but not for auto foo = myFunc(); if myFunc returns a mustUse type.
- giancarlostoro 4y agoThank you, I know it looks stupid simple but code helps me put it into a perspective I can remember better vs only reading documentation about code. That is kind of an interesting way to sort of force someone to not just throw away a return value. Question: Do you know if this would also be fine if the called function was in a return statement?
- Doxin 4y agoThis will only cause an error if both of these are true: - if the expression is the top level expression in a statement (like in my example code) or the left hand side in a comma expression - if the expression is not an assignment, increment, or decrement So using it in a return is fine because then the expression is no longer the top level in the statement, the return is. Of course then the function calling THAT function must deal with not discarding the value. It's pretty neat.
- Aisen8010 4y agoThe official documentation[0] is cryptic as well. [0] https://dlang.org/library/core/attribute/mustuse.html https://dlang.org/library/core/attribute/mustuse.html
- Simon_O_Rourke 4y agoI've always wondered if the Dlang docs are intentionally kept vague and cryptic.
- loeg 4y agoSeems pretty similar to nodiscard in C++17 and previously, GCC attribute warn_unused_result. https://en.cppreference.com/w/cpp/language/attributes/nodiscard https://en.cppreference.com/w/cpp/language/attributes/nodisc... https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute...
- mhh__ 4y agoCryptic in what way? I can fix it but the example given is clear, no?
- TingPing 4y agoI think the example is very clear.
- schveiguy 4y agoOof, seems I got the camelcase wrong. It's `@mustuse` Working on updating that. I think the reason the documentation is cryptic is because it's a library-supplied User Data Attribute that is specially recognized by the compiler. The documentation generator is having trouble with it. In code it's pretty simple: https://github.com/dlang/druntime/blob/705fb36e5fc4d930eed85bf0166273c923117108/src/core/attribute.d#L292 https://github.com/dlang/druntime/blob/705fb36e5fc4d930eed85...
- mhh__ 4y ago@mustUse is a user defined attribute the compiler recognizes that forces return values it affects to be used. e.g. if you return something that could contain an error then ignoring the return value could be disastrous.
- loeg 4y agoA similar concept is used widely in glibc under the name “__wur” (shorthand for __attribute__((warn_unused_result))). https://github.com/bminor/glibc/search?q=__wur&type= https://github.com/bminor/glibc/search?q=__wur&type=
- Georgelemental 4y agoRust also has something similar, in the form of the `#[must_use]` attribute.
- loeg 4y agoYeah, and one called `[[nodiscard]]` in C++17 as well.
- giancarlostoro 4y agoI kind of get this much, but it would be nice to see a small set of code showcasing this in practice to really cement it in for me.
- simcop2387 4y agoIf you want to look at it from an academic perspective it tells the compiler that it should be a relevant[1] type. The idea is that you can make the compiler force the use of the value at least once. This is great for error or result types so that you can require the caller of your code actually check for an error or valid result, this way you can't forget to do it (if you do it's a compile error and you never get to run the code). [1] https://en.wikipedia.org/wiki/Substructural_type_system#Different_substructural_type_systems https://en.wikipedia.org/wiki/Substructural_type_system#Diff...