Last active
April 30, 2021 21:42
-
-
Save EliahKagan/6516957473cbcbaf248bd4ff479cfead to your computer and use it in GitHub Desktop.
Crystal type examples
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
def add(x : T, y : U) forall T, U | |
x + y | |
end | |
p add(1, 2) | |
p add(0.1, 0.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
def add(x : T, y : T) forall T | |
x + y | |
end | |
p add(1, 2) | |
p add(0.1, 0.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
# Originally I had this as add_ by accident and the add(1, 2) call converted | |
# the arguments to floats and called the other one. | |
def add(x : Int32, y : Int32) | |
x + y | |
end | |
def add(x : Float64, y : Float64) | |
x + y | |
end | |
p add(1, 2) | |
p add(0.1, 0.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
def add_integers(x : Int32, y : Int32) | |
x + y | |
end | |
def add_floats(x : Float64, y : Float64) | |
x + y | |
end | |
p add_integers(1, 2) | |
p add_floats(0.1, 0.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
def die(message) | |
STDERR.puts "#{PROGRAM_NAME}: error: #{message}" | |
exit 1 | |
end | |
begin | |
n = ARGV[0].to_i | |
puts "#{n} squared is #{n**2}." | |
rescue IndexError | |
die "too few arguments" | |
rescue ArgumentError | |
die %[can't square "#{ARGV[0]}"] | |
end |
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
def die(message) | |
STDERR.puts "#{PROGRAM_NAME}: error: #{message}" | |
exit 1 | |
end | |
arg = ARGV[0]? | |
puts "arg : #{typeof(arg)}" | |
die "too few arguments" if arg.nil? | |
puts "arg : #{typeof(arg)}" | |
n = arg.to_i? | |
puts "n : #{typeof(n)}" | |
die %[can't square #{ARGV[0]}] if n.nil? | |
puts "n : #{typeof(n)}" | |
puts "#{n} squared is #{n**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
answer = 42 | |
p typeof(answer) | |
answer = ARGV[0] unless ARGV.empty? | |
p typeof(answer) | |
answer = 1.234 if answer.is_a?(Int32) | |
p typeof(answer) |
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
x = 10 | |
if false | |
x = "hello" | |
p x + 1 | |
end | |
p typeof(x) |
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
print "Enter a number to square: " | |
line = gets | |
if line.nil? | |
puts "\nGot end-of-input." | |
exit | |
end | |
n = line.to_i? | |
if n.nil? | |
puts "I can't square that." | |
else | |
puts "That squares to #{n**2}." | |
end |
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
n = ARGV[0].to_u64 | |
n += 1 if ENV.has_key?("INCREMENT") | |
puts "#{n} : #{typeof(n)}" |
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
Copyright (c) 2020 Eliah Kagan <degeneracypressure@gmail.com> | |
Permission to use, copy, modify, and/or distribute this software for any | |
purpose with or without fee is hereby granted. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | |
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
PERFORMANCE OF THIS SOFTWARE. |
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
if ARGV.empty? | |
x = 5 | |
p typeof(x) | |
else | |
x = ARGV[0] | |
p typeof(x) | |
end | |
p typeof(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment