| ... | @@ -248,7 +248,7 @@ pub fn main() !void { | ... | @@ -248,7 +248,7 @@ pub fn main() !void { |
| 248 | </p> | 248 | </p> |
| 249 | <p> | 249 | <p> |
| 250 | Following the <code>hello.zig</code> Zig code sample, the {#link|Zig Build System#} is used | 250 | Following the <code>hello.zig</code> Zig code sample, the {#link|Zig Build System#} is used |
| 251 | to build an executable program from the <code>hello.zig</code> source code. Then, the | 251 | to build an executable program from the <code>hello.zig</code> source code. Then, the |
| 252 | <code>hello</code> program is executed showing its output <code>Hello, world!</code>. The | 252 | <code>hello</code> program is executed showing its output <code>Hello, world!</code>. The |
| 253 | lines beginning with <code>$</code> represent command line prompts and a command. | 253 | lines beginning with <code>$</code> represent command line prompts and a command. |
| 254 | Everything else is program output. | 254 | Everything else is program output. |
| ... | @@ -293,7 +293,7 @@ pub fn main() !void { | ... | @@ -293,7 +293,7 @@ pub fn main() !void { |
| 293 | <p> | 293 | <p> |
| 294 | In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and | 294 | In Zig, a function's block of statements and expressions are surrounded by <code>{</code> and |
| 295 | <code>}</code> curly-braces. Inside of the <code>main</code> function are expressions that perform | 295 | <code>}</code> curly-braces. Inside of the <code>main</code> function are expressions that perform |
| 296 | the task of outputting <code>Hello, world!</code> to standard output. | 296 | the task of outputting <code>Hello, world!</code> to standard output. |
| 297 | </p> | 297 | </p> |
| 298 | <p> | 298 | <p> |
| 299 | First, a constant identifier, <code>stdout</code>, is initialized to represent standard output's | 299 | First, a constant identifier, <code>stdout</code>, is initialized to represent standard output's |
| ... | @@ -5135,6 +5135,22 @@ test "float widening" { | ... | @@ -5135,6 +5135,22 @@ test "float widening" { |
| 5135 | var c: f64 = b; | 5135 | var c: f64 = b; |
| 5136 | var d: f128 = c; | 5136 | var d: f128 = c; |
| 5137 | assert(d == a); | 5137 | assert(d == a); |
| | 5138 | } |
| | 5139 | {#code_end#} |
| | 5140 | {#header_close#} |
| | 5141 | {#header_open|Type Coercion: Coercion Float to Int#} |
| | 5142 | <p> |
| | 5143 | A compiler error is appropriate because this ambiguous expression leaves the compiler |
| | 5144 | two choices about the coercion. |
| | 5145 | <ul> |
| | 5146 | <li> Cast {#syntax#}54.0{#endsyntax#} to {#syntax#}comptime_int{#endsyntax#} resulting in {#syntax#}@as(comptime_int, 10){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10){#endsyntax#}</li> |
| | 5147 | <li> Cast {#syntax#}5{#endsyntax#} to {#syntax#}comptime_float{#endsyntax#} resulting in {#syntax#}@as(comptime_float, 10.8){#endsyntax#}, which is casted to {#syntax#}@as(f32, 10.8){#endsyntax#}</li> |
| | 5148 | </ul> |
| | 5149 | </p> |
| | 5150 | {#code_begin|test_err#} |
| | 5151 | // Compile time coercion of float to int |
| | 5152 | test "implicit cast to comptime_int" { |
| | 5153 | var f: f32 = 54.0 / 5; |
| 5138 | } | 5154 | } |
| 5139 | {#code_end#} | 5155 | {#code_end#} |
| 5140 | {#header_close#} | 5156 | {#header_close#} |
| ... | @@ -8179,7 +8195,7 @@ const expect = std.testing.expect; | ... | @@ -8179,7 +8195,7 @@ const expect = std.testing.expect; |
| 8179 | test "@src" { | 8195 | test "@src" { |
| 8180 | doTheTest(); | 8196 | doTheTest(); |
| 8181 | } | 8197 | } |
| 8182 | | 8198 | |
| 8183 | fn doTheTest() void { | 8199 | fn doTheTest() void { |
| 8184 | const src = @src(); | 8200 | const src = @src(); |
| 8185 | | 8201 | |