Introspection: Difference between revisions

Content added Content deleted
(add RPL)
(Add Zig example)
Line 2,483: Line 2,483:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang="yabasic">if peek("version") < 2.63 error "Interpreter version is too old!"</syntaxhighlight>
<syntaxhighlight lang="yabasic">if peek("version") < 2.63 error "Interpreter version is too old!"</syntaxhighlight>

=={{header|Zig}}==

'''Works with:''' 0.10.1, 0.11.0, 0.12.0-dev.1577+9ad03b628

Note that errors for compiler version check and @hasDecl/@hasField are usually reported at comptime via @compileError, not at runtime, but for demonstrating purposes output is moved to runtime (introspection is still at comptime).

<syntaxhighlight lang="zig">const std = @import("std");
const builtin = @import("builtin");

pub const bloop: i32 = -1_000;

pub fn abs(a: i32) i32 {
return if (a < 0) -a else a;
}

pub fn main() error{NotSupported}!void {
if (builtin.zig_version.order(.{ .major = 0, .minor = 11, .patch = 0 }) == .lt) {
std.debug.print("Version {any} is less than 0.11.0, not suitable, exiting!\n", .{builtin.zig_version});
return error.NotSupported;
} else {
std.debug.print("Version {any} is more or equal than 0.11.0, suitable, continuing!\n", .{builtin.zig_version});
}

if (@hasDecl(@This(), "bloop") and @hasDecl(@This(), "abs")) {
std.debug.print("abs(bloop) = {d}\n", .{abs(bloop)});
} else {
std.debug.print("abs and/or bloop are not defined!\n", .{});
}
}</syntaxhighlight>

{{out|case=Zig version 0.10.1}}
<pre>
Version 0.10.1 is less than 0.11.0, not suitable, exiting!
error: NotSupported
src/instrospection.zig:13:9: 0x211a1e in main (instrospection)
return error.NotSupported;
^
</pre>

{{out|case=Zig version 0.11.0}}
<pre>
Version 0.11.0 is more or equal than 0.11.0, suitable, continuing!
abs(bloop) = 1000
</pre>

{{out|case=Zig version 0.12.0-dev.1577+9ad03b628}}
<pre>
Version 0.12.0-dev.1577+9ad03b628 is more or equal than 0.11.0, suitable, continuing!
abs(bloop) = 1000
</pre>

===Extra credit===

<syntaxhighlight lang="zig">const std = @import("std");

pub const first_integer_constant: i32 = 5;
pub const second_integer_constant: i32 = 3;
pub const third_integer_constant: i32 = -2;

pub const some_non_integer_constant: f32 = 0.0;
pub const another_non_integer_constant: bool = false;

pub fn main() void {
comptime var cnt: comptime_int = 0;
comptime var sum: comptime_int = 0;
inline for (@typeInfo(@This()).Struct.decls) |decl_info| {
const decl = @field(@This(), decl_info.name);
switch (@typeInfo(@TypeOf(decl))) {
.Int, .ComptimeInt => {
sum += decl;
cnt += 1;
},
else => continue,
}
}

@compileLog(std.fmt.comptimePrint("cnt = {d}, sum = {d}", .{ cnt, sum }));
}</syntaxhighlight>

{{out}}
<pre>
src/instrospection.zig:24:5: error: found compile log statement
@compileLog(std.fmt.comptimePrint("cnt = {d}, sum = {d}", .{ cnt, sum }));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(*const [16:0]u8, "cnt = 3, sum = 6")
</pre>


=={{header|zkl}}==
=={{header|zkl}}==