Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 8, 2021
1 parent 2fff5bb commit e905720
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 55 deletions.
12 changes: 12 additions & 0 deletions crystal/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,15 @@ To be able to compare two objects based on their attributes only we can used the
* [Object](https://crystal-lang.org/api/Object.html)

![](examples/classes/compare_objects.cr)

## Singleton using class properties
{id: singleton-using-class-properties}
{i: class_property}

![](examples/classes/singleton_using_class_properties.cr)

## Singleton using class properties with default values
{id: singleton-using-class-properties-with-defaults}
{i: class_property}

![](examples/classes/singleton_using_class_properties_with_defaults.cr)
13 changes: 13 additions & 0 deletions crystal/examples/classes/singleton_using_class_properties.cr
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

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
10 changes: 10 additions & 0 deletions crystal/examples/functions/array_overload.cr
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)
8 changes: 8 additions & 0 deletions crystal/examples/functions/manually_separate.cr
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])
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)
8 changes: 8 additions & 0 deletions crystal/examples/functions/tuple_from.cr
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))
109 changes: 55 additions & 54 deletions crystal/examples/try.cr
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)
26 changes: 25 additions & 1 deletion crystal/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@

* Type definition for parameters
* Return Type definition
* Multi-dispatch functions with same name but different signature
* Multi-dispatch functions with same name but different signature

## Wrong number of arguments
{id: wrong-number-of-arguments}

* We have a function that expect two integers. Can we instead pass an array of two integers?
* Normally no, but there are at least 3 solutions

![](examples/functions/pass_array_instead_of_individual_values.cr)

## Manually separate
{id: manually-separate}

![](examples/functions/manually_separate.cr)

## Tuple from
{id: tuple-from}

![](examples/functions/tuple_from.cr)


## Array overload
{id: array-overload}

![](examples/functions/array_overload.cr)

0 comments on commit e905720

Please sign in to comment.