Skip to content

Commit

Permalink
Removed global variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Ary Borenszweig committed Sep 11, 2016
1 parent 9259fbd commit e8d4843
Show file tree
Hide file tree
Showing 10 changed files with 9 additions and 26 deletions.
1 change: 0 additions & 1 deletion SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* [Assignment](syntax_and_semantics/assignment.md)
* [Multiple assignment](syntax_and_semantics/multiple_assignment.md)
* [Local variables](syntax_and_semantics/local_variables.md)
* [Global variables](syntax_and_semantics/global_variables.md)
* [Union types](syntax_and_semantics/union_types.md)
* [Control expressions](syntax_and_semantics/control_expressions.md)
* [Truthy and falsey values](syntax_and_semantics/truthy_and_falsey_values.md)
Expand Down
2 changes: 0 additions & 2 deletions conventions/coding_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ end
__Variable names__ are underscore-cased. For example:

```crystal
$global_greeting = "Hello world"
class Greeting
@@default_greeting = "Hello world"
Expand Down
2 changes: 1 addition & 1 deletion guides/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The scheduler has a queue of :

### Communicating data

Because at this moment there's only a single thread executing your code, accessing and modifying a global variable in different fibers will work just fine. However, once multiple threads (parallelism) is introduced in the language, it might break. That's why the recommended mechanism to communicate data is using channels and sending messages between them. Internally, a channel implements all the locking mechanisms to avoid data races, but from the outside you use them as communication primitives, so you (the user) don't have to use locks.
Because at this moment there's only a single thread executing your code, accessing and modifying a class variable in different fibers will work just fine. However, once multiple threads (parallelism) is introduced in the language, it might break. That's why the recommended mechanism to communicate data is using channels and sending messages between them. Internally, a channel implements all the locking mechanisms to avoid data races, but from the outside you use them as communication primitives, so you (the user) don't have to use locks.

## Sample code

Expand Down
3 changes: 0 additions & 3 deletions syntax_and_semantics/assignment.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ local = 1
# Assigns to a class variable
@@class = 3
# Assigns to a global variable
$global = 4
```

Each of the above kinds of variables will be explained later on.
Expand Down
2 changes: 1 addition & 1 deletion syntax_and_semantics/closures.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ x = 'a'
x # : Int32 | String | Char
```

This is because the captured block could have been potentially stored in a global, class or instance variable and invoked in a separate thread in between the instructions. The compiler doesn't do an exhaustive analysis of this: it just assumes that if a variable is captured by a proc, the time of that proc invocation is unknown.
This is because the captured block could have been potentially stored in a class or instance variable and invoked in a separate thread in between the instructions. The compiler doesn't do an exhaustive analysis of this: it just assumes that if a variable is captured by a proc, the time of that proc invocation is unknown.

This also happens with regular proc literals, even if it's evident that the proc wasn't invoked or stored:

Expand Down
11 changes: 0 additions & 11 deletions syntax_and_semantics/global_variables.md

This file was deleted.

2 changes: 1 addition & 1 deletion syntax_and_semantics/if_var.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Here, the right-hand side of the `&&` expression is also guaranteed to have `a`

Of course, reassigning a variable inside the `then` branch makes that variable have a new type based on the expression assigned.

The above logic **doesn’t** work with instance variables, class variables or global variables:
The above logic **doesn’t** work with instance variables or class variables:

```crystal
if @a
Expand Down
2 changes: 1 addition & 1 deletion syntax_and_semantics/if_varis_a.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if a.is_a?(String) && b.is_a?(Number)
end
```

The above **doesn’t** work with instance variables, class variables or global variables. To work with these, first assign them to a variable:
The above **doesn’t** work with instance variables or class variables. To work with these, first assign them to a variable:

```crystal
if @a.is_a?(String)
Expand Down
2 changes: 1 addition & 1 deletion syntax_and_semantics/if_varresponds_to.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
end
```

The above **doesn’t** work with instance variables, class variables or global variables. To work with these, first assign them to a variable:
The above **doesn’t** work with instance variables or class variables. To work with these, first assign them to a variable:

```crystal
if @a.responds_to?(:abs)
Expand Down
8 changes: 4 additions & 4 deletions syntax_and_semantics/type_inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ We can quickly see that `@age` is an integer, but we don't know what's the type

As a code base grows, these issues gain more relevance: understanding a project becomes harder, and compile times become unbearable.

For this reason, Crystal needs to know, in an obvious way (as obvious as to a human), the types of instance, [class](class_variables.html) and [global](global_variables.html) variables.
For this reason, Crystal needs to know, in an obvious way (as obvious as to a human), the types of instance, [class](class_variables.html) variables.

There are several ways to let Crystal know this.

Expand All @@ -40,13 +40,13 @@ end

## Don't use an explicit type annotation

If you omit an explicit type annotation the compiler will try to infer the type of instance, class and global variables using a bunch of syntactic rules.
If you omit an explicit type annotation the compiler will try to infer the type of instance and class variables using a bunch of syntactic rules.

For a given instance/class/global variable, when a rule can be applied and a type can be guessed, the type is added to a set. When no more rules can be applied, the inferred type will be the [union](union_types.html) of those types. Additionally, if the compiler infers that an instance variable isn't always initialized, it will also include the [Nil](literals/nil.html) type.
For a given instance/class variable, when a rule can be applied and a type can be guessed, the type is added to a set. When no more rules can be applied, the inferred type will be the [union](union_types.html) of those types. Additionally, if the compiler infers that an instance variable isn't always initialized, it will also include the [Nil](literals/nil.html) type.

The rules are many, but usually the first three are most used. There's no need to remember them all. If the compiler gives an error saying that the type of an instance variable can't be inferred you can always add an explicit type annotation.

The following rules only mention instance variables, but they apply to class and global variables as well. They are:
The following rules only mention instance variables, but they apply to class variables as well. They are:

### 1. Assigning a literal value

Expand Down

0 comments on commit e8d4843

Please sign in to comment.