Skip to content

Commit a795e4c

Browse files
committedJan 31, 2018
add some docs for reflection
1 parent 44f38b0 commit a795e4c

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed
 

‎doc/langref.html.in

+53-3
Original file line numberDiff line numberDiff line change
@@ -2785,6 +2785,16 @@ test "implicitly cast to const pointer" {
27852785
use the C calling convention may pass structs and unions by value.
27862786
</p>
27872787
{#header_close#}
2788+
{#header_open|Function Reflection#}
2789+
{#code_begin|test#}
2790+
const assert = @import("std").debug.assert;
2791+
2792+
test "fn reflection" {
2793+
assert(@typeOf(assert).ReturnType == void);
2794+
assert(@typeOf(assert).is_var_args == false);
2795+
}
2796+
{#code_end#}
2797+
{#header_close#}
27882798
{#header_close#}
27892799
{#header_open|Errors#}
27902800
<p>
@@ -2998,6 +3008,31 @@ fn createFoo(param: i32) %Foo {
29983008
</li>
29993009
</ul>
30003010
{#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#}
3015+
const assert = @import("std").debug.assert;
3016+
3017+
error SomeError;
3018+
3019+
test "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#}
30013036
{#header_close#}
30023037
{#header_open|Nullables#}
30033038
<p>
@@ -3099,6 +3134,24 @@ fn doAThing(nullable_foo: ?&Foo) void {
30993134
The optimizer can sometimes make better decisions knowing that pointer arguments
31003135
cannot be null.
31013136
</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#}
3141+
const assert = @import("std").debug.assert;
3142+
3143+
test "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#}
31023155
{#header_close#}
31033156
{#header_open|Casting#}
31043157
<p>TODO: explain implicit vs explicit casting</p>
@@ -5731,9 +5784,6 @@ ContainerDecl = option("extern" | "packed")
57315784
<li>Together we serve end users.</li>
57325785
</ul>
57335786
{#header_close#}
5734-
{#header_open|TODO#}
5735-
<p>TODO: document changes from a31b23c46ba2a8c28df01adc1aa0b4d878b9a5cf (compile time reflection additions)</p>
5736-
{#header_close#}
57375787
</div>
57385788
<script>
57395789
/*! highlight.js v9.12.0 | BSD3 License | git.io/hljslicense */

0 commit comments

Comments
 (0)
Please sign in to comment.