crystal-lang


How to use union in “if” statement [Crystal]


Following code works well and print "5.0"
$x : Float64
$y : Float64
$x = 3.0_f64
$y = 2.0_f64
puts $x + $y
Now, I change the code to support "nil".
$x : Float64?
$y : Float64?
$x = 3.0_f64
$y = 2.0_f64
puts $x + $y if !$x.nil? && !$y.nil?
However this code reports the following error message.
no overload matches 'Float64#+' with type (Float64 | Nil)
Overloads are:
- Float64#+(other : Int8)
- Float64#+(other : Int16)
- Float64#+(other : Int32)
- Float64#+(other : Int64)
- Float64#+(other : UInt8)
- Float64#+(other : UInt16)
- Float64#+(other : UInt32)
- Float64#+(other : UInt64)
- Float64#+(other : Float32)
- Float64#+(other : Float64)
- Number#+()
Couldn't find overloads for these types:
- Float64#+(Nil)
puts $x + $y if !$x.nil? && !$y.nil?
I would like to stop the call of method "#+()" if $x or $y is nil
and print the calculated result if both are Float64.
What is the best practice for this situation?
In above code, I simplified the code for this question.
In the result, the meaning of question was changed involuntarily..
I wanted to ask following code actually.
class Xyz
property a, b
#a : Float64?
#b : Float64?
def initialize
#a = nil
#b = nil
end
def do_calc
if !#a.nil? && !#b.nil?
puts #a + #b
else
puts "We can't calculate because '#a or #b has nil."
end
end
end
x = Xyz.new
x.a = 3.0_f64
x.b = 2.0_f64
x.do_calc
This code, reports following error.
instantiating 'Xyz#do_calc()'
x.do_calc
^~~~~~~
in ./a.cr:15: no overload matches 'Float64#+' with type (Float64 | Nil)
Overloads are:
- Float64#+(other : Int8)
- Float64#+(other : Int16)
- Float64#+(other : Int32)
- Float64#+(other : Int64)
- Float64#+(other : UInt8)
- Float64#+(other : UInt16)
- Float64#+(other : UInt32)
- Float64#+(other : UInt64)
- Float64#+(other : Float32)
- Float64#+(other : Float64)
- Number#+()
Couldn't find overloads for these types:
- Float64#+(Nil)
puts #a + #b
How can I avoid this error?
Be sure to read the docs about if, and checking for nil: https://crystal-lang.org/docs/syntax_and_semantics/if_var.html and https://crystal-lang.org/docs/syntax_and_semantics/if_var_nil.html
This only applies to local variables, so you'll need to assign the values to local variables first.
As a side note, global variables don't exist anymore in the language as of Crystal 0.19.0.

Related Links

execution of command failed with code: 1: `cc -o "/root/.cache/crystal/var-app-staging-new-http.cr
How to use union in “if” statement [Crystal]
How to convert a hex string to bytes array in Crystal?
is INSTANCE = new in class is run after compilation automatically or the first time it is accessed?
Crystal lang: understanding method return type of Void
Crystal-lang httpget basic_auth
How to control using CPU cores [Crystal]
How can I read a slice with other size in crystal?
Unix-style password readline
Cannot find -lxml2 when trying to run crystal spec
How do I access an object's eigenclass in Crystal?
Why does my code never reaches the end when waiting for pipe?
Crystal reading x bytes from file
Getting the first element of an empty Array
Errors when running Crystal Play
Shorthand block syntax in Crystal

Categories

HOME
service-worker
forms
fpga
twig
angular2-material
rsa
socket.io
scripting
onclick
hpc
google-cloud-datalab
trac
lanczos
project
vimdiff
mel
android-securityexception
activecollab
redis-sentinel
openbugs
code-generation
flash-player
esoteric-languages
pyramid
swagger-2.0
simpleitk
angular-routing
exit
constexpr
utorrent
testbed
catch-unit-test
rselenium
android-sugarorm
card.io
gojs
fossil
carriage-return
loading
hackintosh
subclipse
dbcontext
rserve
objectscript
jtree
filenet
pure-css
autosar
builtins
actor-platform
seek
lrs
dotnetnuke-7
file-uri
resourcemanager
synonym
spreadjs
uiactionsheet
beanstalk
grass
oracle-spatial
google-books
dock
visual-sourcesafe-2005
botan
matlab-deployment
mruby
machine-instruction
verold
cancellation
draw2d-js
syndicationfeed
spring-3
http-patch
windows-phone-8-sdk
epiceditor
preload
asplinkbutton
resource-management
database-create
jsr286
agility.js
screen-size
creole
drupal-fivestar
wcf-instancing
feasibility
lobo-cobra
dojo-dnd
wspbuilder

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App