-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
151 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
crystal/examples/classes/singleton_using_class_properties.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Options | ||
class_property repetition : Int32|Nil | ||
class_property url = String | ||
class_property verbose = Bool | ||
end | ||
|
||
puts Options.repetition | ||
Options.repetition = 3 | ||
puts Options.repetition | ||
if ! Options.repetition.nil? | ||
puts Options.repetition + 1 | ||
end | ||
|
9 changes: 9 additions & 0 deletions
9
crystal/examples/classes/singleton_using_class_properties_with_defaults.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Options | ||
class_property repetition = 0 | ||
class_property url = "" | ||
class_property verbose = false | ||
end | ||
puts Options.repetition | ||
Options.repetition = 3 | ||
puts Options.repetition | ||
puts Options.repetition + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
def f(x, y) | ||
return x + y | ||
end | ||
def f(args : Array(Int32)) | ||
return f(args[0], args[1]) | ||
end | ||
puts f(2, 3) | ||
values = [3, 4] | ||
puts values | ||
puts f(values) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def f(x, y) | ||
return x + y | ||
end | ||
puts f(2, 3) | ||
values = [3, 4] | ||
puts values | ||
|
||
puts f(values[0], values[1]) |
11 changes: 11 additions & 0 deletions
11
crystal/examples/functions/pass_array_instead_of_individual_values.cr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def f(x, y) | ||
return x + y | ||
end | ||
puts f(2, 3) | ||
values = [3, 4] | ||
puts values | ||
|
||
puts f(values[0], values[1]) | ||
|
||
# puts f(values) | ||
# Error: wrong number of arguments for 'f' (given 1, expected 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
def f(x, y) | ||
return x + y | ||
end | ||
puts f(2, 3) | ||
values = [3, 4] | ||
puts values | ||
|
||
puts f(*{Int32, Int32}.from(values)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,57 @@ | ||
|
||
person = { | ||
"name" => "Foo Bar", | ||
"number" => 42, | ||
} | ||
p! person | ||
puts typeof(person) # Hash(String, Int32 | String) | ||
|
||
joe = {} of String => Int32 | String | ||
joe["name"] = "Joe" | ||
joe["number"] = 23 | ||
# joe["float"] = 2.3 | ||
# compile time error | ||
# Error: no overload matches 'Hash(String, Int32 | String)#[]=' with types String, Float64 | ||
puts typeof(joe) | ||
|
||
|
||
#alias Some = {} of String => Int32 | String | ||
|
||
numbers = [] of Int32 | ||
numbers.push(23) | ||
# numbers.push("text") | ||
# Error: no overload matches 'Array(Int32)#push' with type String | ||
# numbers.push(nil) | ||
# Error: no overload matches 'Array(Int32)#push' with type Nil | ||
puts typeof(numbers) | ||
|
||
alias Int32orNil = Int32|Nil | ||
# nilnum = [] of Int32 | Nil | ||
nilnum = [] of Int32orNil | ||
nilnum.push(23) | ||
nilnum.push(nil) | ||
puts typeof(nilnum) | ||
#num = Int32 | ||
#num = 23 | ||
#num = nil | ||
|
||
# class Person | ||
# name: {type: String} | ||
# number: {type: Int32} | ||
# end | ||
|
||
#alias Person = {name: String, number: Int32} | ||
alias Person = NamedTuple(name: String, number: Int32) | ||
|
||
people = [] of Person | ||
puts typeof(people) | ||
people.push({ | ||
"name": "Foo Bar", | ||
"number": 42 | ||
}) | ||
|
||
puts people[0]["name"] | ||
# people[0]["name"] = "New Name" | ||
# Error: undefined method '[]=' for NamedTuple(name: String, number: Int32) | ||
|
||
|
||
# person = { | ||
# "name" => "Foo Bar", | ||
# "number" => 42, | ||
# } | ||
# p! person | ||
# puts typeof(person) # Hash(String, Int32 | String) | ||
|
||
# joe = {} of String => Int32 | String | ||
# joe["name"] = "Joe" | ||
# joe["number"] = 23 | ||
# # joe["float"] = 2.3 | ||
# # compile time error | ||
# # Error: no overload matches 'Hash(String, Int32 | String)#[]=' with types String, Float64 | ||
# puts typeof(joe) | ||
|
||
|
||
# #alias Some = {} of String => Int32 | String | ||
|
||
# numbers = [] of Int32 | ||
# numbers.push(23) | ||
# # numbers.push("text") | ||
# # Error: no overload matches 'Array(Int32)#push' with type String | ||
# # numbers.push(nil) | ||
# # Error: no overload matches 'Array(Int32)#push' with type Nil | ||
# puts typeof(numbers) | ||
|
||
# alias Int32orNil = Int32|Nil | ||
# # nilnum = [] of Int32 | Nil | ||
# nilnum = [] of Int32orNil | ||
# nilnum.push(23) | ||
# nilnum.push(nil) | ||
# puts typeof(nilnum) | ||
# #num = Int32 | ||
# #num = 23 | ||
# #num = nil | ||
|
||
# # class Person | ||
# # name: {type: String} | ||
# # number: {type: Int32} | ||
# # end | ||
|
||
# #alias Person = {name: String, number: Int32} | ||
# alias Person = NamedTuple(name: String, number: Int32) | ||
|
||
# people = [] of Person | ||
# puts typeof(people) | ||
# people.push({ | ||
# "name": "Foo Bar", | ||
# "number": 42 | ||
# }) | ||
|
||
# puts people[0]["name"] | ||
# # people[0]["name"] = "New Name" | ||
# # Error: undefined method '[]=' for NamedTuple(name: String, number: Int32) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters