From 180e650cb220ac27e8ca5f7827ab609078f591a5 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 5 Jun 2014 00:43:00 +0100 Subject: [PATCH 1/4] Create type_casting_inference.md --- content/variables/type_casting_inference.md | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 content/variables/type_casting_inference.md diff --git a/content/variables/type_casting_inference.md b/content/variables/type_casting_inference.md new file mode 100644 index 0000000..7bd2544 --- /dev/null +++ b/content/variables/type_casting_inference.md @@ -0,0 +1,35 @@ +--- +title: 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. + +```nimrod +# These types are inferred. +var x = 5 # int +var y = "foo" # string + +# Assigning a value of a different type will result in a compile-time error. +x = y +``` + +```console +$ 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. + +```nimrod +var x: int = int(1 / 3) # Type conversion. `/` returns a Float. The `: int` is superfluous. + +var y = "Foobar" +proc ffi(foo: ptr array[6, char]) = echo repr(foo) +ffi(cast[ptr array[6, char]](addr y[0])) +``` + +```console +$ nimrod c -r typecasting.nim +ref 002C8030 --> ['F', 'o', 'o', 'b', 'a', 'r'] +``` From 7afd4467b0da32f88cb9a2757f0c9bc81cff6076 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 5 Jun 2014 00:43:46 +0100 Subject: [PATCH 2/4] Add type_casting_inference to toc. --- content/toc.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/content/toc.md b/content/toc.md index e4de466..8edb8a1 100644 --- a/content/toc.md +++ b/content/toc.md @@ -3,6 +3,7 @@ * [Getting Started](/getting_started/) * [Hello World](/hello_world/) * [Variables](/variables/page/) + * [Type casting and inference](/variables/type_casting_inference) * [Result](/variables/result/) * [Objects](/objects/) * [Enums](/enums/) @@ -13,4 +14,4 @@ * [Procedures](/procs/) * [Exceptions](/exceptions/) * [Varargs](/varargs/) -* [OOP Macro](/oop_macro/) \ No newline at end of file +* [OOP Macro](/oop_macro/) From e2d252aaa989c0bc0381dd06d897fc8fe00e57bf Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 5 Jun 2014 00:49:28 +0100 Subject: [PATCH 3/4] Add trailing slash to toc link. --- content/toc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/toc.md b/content/toc.md index 8edb8a1..b75f59e 100644 --- a/content/toc.md +++ b/content/toc.md @@ -3,7 +3,7 @@ * [Getting Started](/getting_started/) * [Hello World](/hello_world/) * [Variables](/variables/page/) - * [Type casting and inference](/variables/type_casting_inference) + * [Type casting and inference](/variables/type_casting_inference/) * [Result](/variables/result/) * [Objects](/objects/) * [Enums](/enums/) From 3c83947464f139f049ec926f1afc2c5fa5c5617f Mon Sep 17 00:00:00 2001 From: Flaviu Tamas Date: Wed, 4 Jun 2014 19:53:16 -0400 Subject: [PATCH 4/4] Flatten TOC --- content/toc.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/toc.md b/content/toc.md index b75f59e..eb29646 100644 --- a/content/toc.md +++ b/content/toc.md @@ -3,8 +3,8 @@ * [Getting Started](/getting_started/) * [Hello World](/hello_world/) * [Variables](/variables/page/) - * [Type casting and inference](/variables/type_casting_inference/) - * [Result](/variables/result/) +* [Result](/variables/result/) +* [Type Casting and Inference](/variables/type_casting_inference/) * [Objects](/objects/) * [Enums](/enums/) * [If, Else, While, Block](/if_else_while/)