authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-31 11:47:56-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-31 11:47:56-05:00
loga795e4ce32d0f0806a0cef7ca6bbc45533b77c61
treee4975c538633ec83bf44ed782734aae3fc4bfbbf
parent44f38b04b0cc374fcd377df0fe68f29c824185ff

add some docs for reflection


1 files changed, 53 insertions(+), 3 deletions(-)

doc/langref.html.in+53-3
...@@ -2785,6 +2785,16 @@ test "implicitly cast to const pointer" {...@@ -2785,6 +2785,16 @@ test "implicitly cast to const pointer" {
2785 use the C calling convention may pass structs and unions by value.2785 use the C calling convention may pass structs and unions by value.
2786 </p>2786 </p>
2787 {#header_close#}2787 {#header_close#}
2788 {#header_open|Function Reflection#}
2789 {#code_begin|test#}
2790const assert = @import("std").debug.assert;
2791
2792test "fn reflection" {
2793 assert(@typeOf(assert).ReturnType == void);
2794 assert(@typeOf(assert).is_var_args == false);
2795}
2796 {#code_end#}
2797 {#header_close#}
2788 {#header_close#}2798 {#header_close#}
2789 {#header_open|Errors#}2799 {#header_open|Errors#}
2790 <p>2800 <p>
...@@ -2998,6 +3008,31 @@ fn createFoo(param: i32) %Foo {...@@ -2998,6 +3008,31 @@ fn createFoo(param: i32) %Foo {
2998 </li>3008 </li>
2999 </ul>3009 </ul>
3000 {#see_also|defer|if|switch#}3010 {#see_also|defer|if|switch#}
3011 {#header_open|Error Union Type#}
3012 <p>An error union is created by putting a <code>%</code> in front of a type.
3013 You can use compile-time reflection to access the child type of an error union:</p>
3014 {#code_begin|test#}
3015const assert = @import("std").debug.assert;
3016
3017error SomeError;
3018
3019test "error union" {
3020 var foo: %i32 = undefined;
3021
3022 // Implicitly cast from child type of an error union:
3023 foo = 1234;
3024
3025 // Implicitly cast from an error set:
3026 foo = error.SomeError;
3027
3028 // Use compile-time reflection to access the child type of an error union:
3029 comptime assert(@typeOf(foo).Child == i32);
3030}
3031 {#code_end#}
3032 {#header_close#}
3033 {#header_open|Error Set Type#}
3034 <p>TODO</p>
3035 {#header_close#}
3001 {#header_close#}3036 {#header_close#}
3002 {#header_open|Nullables#}3037 {#header_open|Nullables#}
3003 <p>3038 <p>
...@@ -3099,6 +3134,24 @@ fn doAThing(nullable_foo: ?&Foo) void {...@@ -3099,6 +3134,24 @@ fn doAThing(nullable_foo: ?&Foo) void {
3099 The optimizer can sometimes make better decisions knowing that pointer arguments3134 The optimizer can sometimes make better decisions knowing that pointer arguments
3100 cannot be null.3135 cannot be null.
3101 </p>3136 </p>
3137 {#header_open|Nullable Type#}
3138 <p>A nullable is created by putting <code>?</code> in front of a type. You can use compile-time
3139 reflection to access the child type of a nullable:</p>
3140 {#code_begin|test#}
3141const assert = @import("std").debug.assert;
3142
3143test "nullable type" {
3144 // Declare a nullable and implicitly cast from null:
3145 var foo: ?i32 = null;
3146
3147 // Implicitly cast from child type of a nullable
3148 foo = 1234;
3149
3150 // Use compile-time reflection to access the child type of the nullable:
3151 comptime assert(@typeOf(foo).Child == i32);
3152}
3153 {#code_end#}
3154 {#header_close#}
3102 {#header_close#}3155 {#header_close#}
3103 {#header_open|Casting#}3156 {#header_open|Casting#}
3104 <p>TODO: explain implicit vs explicit casting</p>3157 <p>TODO: explain implicit vs explicit casting</p>
...@@ -5731,9 +5784,6 @@ ContainerDecl = option("extern" | "packed")...@@ -5731,9 +5784,6 @@ ContainerDecl = option("extern" | "packed")
5731 <li>Together we serve end users.</li>5784 <li>Together we serve end users.</li>
5732 </ul>5785 </ul>
5733 {#header_close#}5786 {#header_close#}
5734 {#header_open|TODO#}
5735 <p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p>
5736 {#header_close#}
5737 </div>5787 </div>
5738 <script>5788 <script>
5739/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */5789/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */