From e9057203625a46583607d634f4f801ad6279c30a Mon Sep 17 00:00:00 2001 From: Gabor Szabo Date: Tue, 8 Jun 2021 14:34:19 +0300 Subject: [PATCH] examples --- crystal/classes.md | 12 ++ .../singleton_using_class_properties.cr | 13 +++ ...on_using_class_properties_with_defaults.cr | 9 ++ crystal/examples/functions/array_overload.cr | 10 ++ .../examples/functions/manually_separate.cr | 8 ++ ...pass_array_instead_of_individual_values.cr | 11 ++ crystal/examples/functions/tuple_from.cr | 8 ++ crystal/examples/try.cr | 109 +++++++++--------- crystal/functions.md | 26 ++++- 9 files changed, 151 insertions(+), 55 deletions(-) create mode 100644 crystal/examples/classes/singleton_using_class_properties.cr create mode 100644 crystal/examples/classes/singleton_using_class_properties_with_defaults.cr create mode 100644 crystal/examples/functions/array_overload.cr create mode 100644 crystal/examples/functions/manually_separate.cr create mode 100644 crystal/examples/functions/pass_array_instead_of_individual_values.cr create mode 100644 crystal/examples/functions/tuple_from.cr diff --git a/crystal/classes.md b/crystal/classes.md index ce43fdd08..3f855336f 100644 --- a/crystal/classes.md +++ b/crystal/classes.md @@ -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) diff --git a/crystal/examples/classes/singleton_using_class_properties.cr b/crystal/examples/classes/singleton_using_class_properties.cr new file mode 100644 index 000000000..5982e731c --- /dev/null +++ b/crystal/examples/classes/singleton_using_class_properties.cr @@ -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 + diff --git a/crystal/examples/classes/singleton_using_class_properties_with_defaults.cr b/crystal/examples/classes/singleton_using_class_properties_with_defaults.cr new file mode 100644 index 000000000..32ead782b --- /dev/null +++ b/crystal/examples/classes/singleton_using_class_properties_with_defaults.cr @@ -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 diff --git a/crystal/examples/functions/array_overload.cr b/crystal/examples/functions/array_overload.cr new file mode 100644 index 000000000..bc7c0d7fa --- /dev/null +++ b/crystal/examples/functions/array_overload.cr @@ -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) diff --git a/crystal/examples/functions/manually_separate.cr b/crystal/examples/functions/manually_separate.cr new file mode 100644 index 000000000..d838afe5f --- /dev/null +++ b/crystal/examples/functions/manually_separate.cr @@ -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]) diff --git a/crystal/examples/functions/pass_array_instead_of_individual_values.cr b/crystal/examples/functions/pass_array_instead_of_individual_values.cr new file mode 100644 index 000000000..3d936a196 --- /dev/null +++ b/crystal/examples/functions/pass_array_instead_of_individual_values.cr @@ -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) diff --git a/crystal/examples/functions/tuple_from.cr b/crystal/examples/functions/tuple_from.cr new file mode 100644 index 000000000..467199de6 --- /dev/null +++ b/crystal/examples/functions/tuple_from.cr @@ -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)) diff --git a/crystal/examples/try.cr b/crystal/examples/try.cr index 0ece7d235..4d282a4e5 100644 --- a/crystal/examples/try.cr +++ b/crystal/examples/try.cr @@ -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) diff --git a/crystal/functions.md b/crystal/functions.md index 59c791fca..0b5ef4fc5 100644 --- a/crystal/functions.md +++ b/crystal/functions.md @@ -22,4 +22,28 @@ * Type definition for parameters * Return Type definition -* Multi-dispatch functions with same name but different signature \ No newline at end of file +* 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)