From 00bcb529736bcaeb1314a54a9d194abcd4ec044b Mon Sep 17 00:00:00 2001 From: flaviut Date: Thu, 19 Jun 2014 23:39:54 -0400 Subject: [PATCH] Reword casting and inference --- content/variables/type_casting_inference.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/content/variables/type_casting_inference.md b/content/variables/type_casting_inference.md index 7bd2544..82fef5c 100644 --- a/content/variables/type_casting_inference.md +++ b/content/variables/type_casting_inference.md @@ -1,7 +1,7 @@ --- -title: Type casting and inference +title: Type Casting and Inference --- -# Type casting and inference +# Type Casting and Inference Nimrod is a statically typed language. As such, each variable has a type associated with it. As seen in the previous example these types are inferred in the `const`, `let` and `var` declarations by the compiler. @@ -19,10 +19,13 @@ $ nimrod c -r typeinference.nim typeinference.nim(6, 4) Error: type mismatch: got (string) but expected 'int' ``` -You may optionally specify the type after a colon (`:`). In some cases the compiler will expect you to explicitly cast types though. This can be done in two ways. One is to use "type conversion", this is the safe method, the compiler will ensure that it is safe to convert to that type. The other is to use the `cast` keyword, this is the unsafe method and should only be used when working with hardware or interfacing with C. +You may optionally specify the type after a colon (`:`). In some cases the compiler will expect you to explicitly cast types, for which two ways are available: + + - type conversion, whose safety checked by the compiler + - the `cast` keyword, which is unsafe and should be used only where you know what you are doing, such as in interfacing with C ```nimrod -var x: int = int(1 / 3) # Type conversion. `/` returns a Float. The `: int` is superfluous. +var x = int(1.0 / 3) # type conversion var y = "Foobar" proc ffi(foo: ptr array[6, char]) = echo repr(foo)