| ... | @@ -5010,6 +5010,20 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val | ... | @@ -5010,6 +5010,20 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val |
| 5010 | <p>Returns whether or not a {#link|Namespace#} has a declaration matching {#syntax#}name{#endsyntax#}.</p> | 5010 | <p>Returns whether or not a {#link|Namespace#} has a declaration matching {#syntax#}name{#endsyntax#}.</p> |
| 5011 | {#code|test_hasDecl_builtin.zig#} | 5011 | {#code|test_hasDecl_builtin.zig#} |
| 5012 | | 5012 | |
| | 5013 | <p>Caution: using {#syntax#}@hasDecl{#endsyntax#} to implement |
| | 5014 | conditional compilation based on the presence or absence of declarations |
| | 5015 | in an API runs the risk of introducing dead code into the codebase, |
| | 5016 | unnoticed. For example, if the field is ever renamed, code which uses |
| | 5017 | {#syntax#}@hasDecl{#endsyntax#} will compile successfully while failing |
| | 5018 | to enable or disable an intended block of code. Even a simple a typo in |
| | 5019 | the field name passed to {#syntax#}@hasDecl{#endsyntax#} would go |
| | 5020 | unnoticed by the compiler. Thus, it is safer to use alternative methods |
| | 5021 | to check for the presence or absence of a particular declaration where |
| | 5022 | possible. For example, if the target operating system would indicate whether a particular |
| | 5023 | function is available, it more maintainable to check |
| | 5024 | {#syntax#}@import("builtin").target.os.tag{#endsyntax#} rather than |
| | 5025 | {#syntax#}@hasDecl{#endsyntax#}.</p> |
| | 5026 | |
| 5013 | {#see_also|@hasField#} | 5027 | {#see_also|@hasField#} |
| 5014 | {#header_close#} | 5028 | {#header_close#} |
| 5015 | | 5029 | |