# Appendix C: Coming from Ruby, Crystal or C#

> Appendix C: a table of the places where Alice differs from Ruby, Crystal and C#, with the Alice way to write each and the chapter that explains it.

Canonical: https://alice-lang.org/documentation/learn/from-ruby/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

Alice borrows its syntax from Ruby and Crystal and its type system from .NET. Most code reads the way a Ruby or Crystal programmer expects. This appendix lists where it does not. Each row is checked against the current compiler, and the chapter column points to the explanation.

## From Ruby

| In Ruby                                   | In Alice                                                                                  | Chapter                                                               |
| ----------------------------------------- | ----------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
| a variable can hold any value             | a variable keeps the type of its first value (`AL0304`)                                   | [3](https://alice-lang.org/documentation/learn/common-concepts.md)    |
| integers grow without limit               | `Int32` and `Int64` raise `OverflowException`; `&+ &- &*` wrap on purpose                 | [3](https://alice-lang.org/documentation/learn/common-concepts.md)    |
| `-7 / 2 == -4`                            | the same: division rounds down, as in Ruby                                                | [3](https://alice-lang.org/documentation/learn/common-concepts.md)    |
| `nil` can appear anywhere                 | only `T?` types hold `nil`, and must be checked (`AL0310`)                                | [3](https://alice-lang.org/documentation/learn/common-concepts.md)    |
| `x = nil`                                 | needs a type: `x : String? = nil` (`AL0204`)                                              | [3](https://alice-lang.org/documentation/learn/common-concepts.md)    |
| `"abc".to_i` is `0`                       | the same; `to_i?` returns `nil` for text that is not a number                             | [2](https://alice-lang.org/documentation/learn/guessing-game.md)      |
| `xs << 1` on an array                     | arrays have a fixed size; use `List(T)`                                                   | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| `xs[-1]` is the last element              | the same for a literal index (since 1.0); a computed negative index raises, use `xs.last` | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| `xs[1..]`, `s[1..3]` slices               | the same (since 1.0), on arrays, lists and strings                                        | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| `h["missing"]` is `nil`                   | raises `KeyNotFoundException`; use `fetch`, `key?` or `Hash(K, V).new(default)`           | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| `s << "x"`, `s[0] = "A"`                  | strings are immutable; build a new string                                                 | [5](https://alice-lang.org/documentation/learn/strings.md)            |
| `"a" < "b"`                               | not defined on strings; use `<=>`                                                         | [5](https://alice-lang.org/documentation/learn/strings.md)            |
| `{a: 1}` is a hash                        | a named tuple; hashes use `=>`                                                            | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| `MENU = {...}`                            | a constant collection needs its type: `MENU : Hash(String, Int32) = {...}`                | [5](https://alice-lang.org/documentation/learn/strings.md)            |
| `Array<Int32>` (Crystal)                  | generics use parentheses: `Array(Int32)`                                                  | [4](https://alice-lang.org/documentation/learn/collections.md)        |
| constructors are inherited                | each subclass writes `initialize` and calls `super`                                       | [6](https://alice-lang.org/documentation/learn/types.md)              |
| `require "json"` loads a gem              | `require` loads `.al` files; libraries are NuGet packages                                 | [14](https://alice-lang.org/documentation/learn/dotnet.md)            |
| threads share anything                    | data races are compile errors (`AL0202`)                                                  | [10](https://alice-lang.org/documentation/learn/safety.md)            |
| `method_missing`, `send`, `define_method` | supported, compiled statically where possible, otherwise through reflection               | [reference](https://alice-lang.org/documentation/language/dynamic.md) |

The things that work as in Ruby are the majority: `def`/`end`, blocks with `{ }` and `do ... end`, `|x|` parameters, `it` and `_1`, `&:name`, implicit returns, `?` predicates, `unless`, postfix `if`, `case`/`when`, `case`/`in` patterns, `begin`/`rescue`/`ensure`/`retry`, the `rescue` modifier, heredocs, `%w[]`, string interpolation, `Comparable` and `Enumerable` mixins, and most of the core methods of `Array`, `Hash`, `String` and `Integer`.

## From Crystal

Crystal programmers will find types, `getter`/`property`, `record`, `struct`, `T?`, generics and `forall` familiar. The differences are mostly about the platform:

- Alice runs on .NET: the standard library is the .NET base library plus Alice's Prelude, and packages come from NuGet.
- Method calls on .NET types keep their .NET names (`sb.Append`), with Alice spellings where they map (`sb.to_s`).
- Macros are not part of the language.
- Concurrency is checked: `spawn` and `Channel` exist, and the compiler rejects data races.

## From C\#

| C#                                              | Alice                                                   |
| ----------------------------------------------- | ------------------------------------------------------- |
| `int`, `long`, `double`, `string`, `bool`       | `Int32`, `Int64`, `Float64`, `String`, `Bool`           |
| `int[]`, `List<int>`, `Dictionary<string, int>` | `Array(Int32)`, `List(Int32)`, `Hash(String, Int32)`    |
| `new Point(1, 2)`                               | `Point.new(1, 2)`                                       |
| `x => x * 2`                                    | `->(x : Int32) { x * 2 }`, or a block `{ \|x\| x * 2 }` |
| `-7 / 2 == -3`                                  | `-7 / 2 == -4`; `tdiv` truncates like C#                |
| `unchecked` overflow by default                 | overflow raises; `unchecked ... end` or `&+` wraps      |
| `string?` warnings                              | `String?` errors (`AL0310`)                             |
| `get; set;`                                     | `getter`, `property`, `setter`                          |
| `record Point(int X, int Y)`                    | `record Point(x : Int32, y : Int32)`                    |
| `switch` with patterns                          | `case ... in`, with exhaustiveness checks (`AL0201`)    |
| `async Task<T>`                                 | `async def`, with the `Task` return type inferred       |
| `[DllImport]`                                   | `lib` and `fun` with `@[Link]`                          |
| `Parallel.For` with a shared variable           | rejected at compile time (`AL0202`)                     |

Alice types are ordinary CLR types, so values cross between Alice and C# without conversion, in both directions ([chapter 14](https://alice-lang.org/documentation/learn/dotnet.md)).

## Continue reading

- Previous: [Appendix B: Error codes](https://alice-lang.org/documentation/learn/error-codes.md)
- Next: [Appendix D: Standard library index](https://alice-lang.org/documentation/learn/library-index.md)
