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

Make Class member attribute of type Array(T) accept 2D arrays of T
JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20
Application server for Crystal web app / Kemal
Crystal Fiber error - “Unhandled exception in spawn: SSL_shutdown: Operation now in progress (Errno)”
Convert hex string to int in Crystal
Crystal convert string to hash from response.body
Crystal no overload matches 'Array(Type)#[]' with type (Int32 | Nil)
Crystal method return multiple values
Reopening class or struct inside macro

Categories

HOME
httparty
gridview
jpa
ggplot2
static
camera
carousel
joomla3.0
extjs4.2
categorical-data
pega
mautic
opengl-es-3.0
favicon
frequency-distribution
dynatrace
flash-player
keytool
symfony-2.8
sse
breadth-first-search
kendo-mobile
guzzle
href
r-mice
roslyn
mongoid6
intersystems-cache
bioinformatics
realm-mobile-platform
calculation
python-import
protege4
google-earth-engine
reactivekit
timing
google-data-api
chef-solo
kill
azure-arm
odoo-website
dbnull
apollostack
angular-http
facebook-social-plugins
openblas
context-switch
context-sensitive-grammar
yii2-user
mach-o
lsyncd
gpg-signature
slackware
atlas
drawstring
php-socket
turbo-prolog
exim4
design-principles
vigenere
.bash-profile
haskell-warp
showdialog
beatsmusic
post-commit-hook
device-tree
encryption-asymmetric
ora-00904
websphere-esb
multiautocompletetextview
windows-embedded-standard
high-resolution
traceability
video-codecs
argouml
device-width
dajaxice
iboutlet
paginator
wiimote
expression-blend-4
http-daemon
instantiationexception
drupal-fivestar
openfaces
sifr
qtabbar
interop-domino
application-planning
pbcopy
idatareader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App