Page 1 of 1

How to ADD inputs in a program

PostPosted: Sat Feb 07, 2015 9:36 pm
by alisterescabarte
class Add

def main
print 'Enter two numbers:'
x = Console.readLine
y = Console.readLine
print 'sum is', int.parse(x) + int.parse(y)
print 'done.'


that is the code i used when compiling in xamarin studio cobra language'

but i wonder why i get this error when compiling === Error: Argument 1 of method "parse" expects a non-nilable type (String), but the call is supplying a nilable type (String?). The declaration is "parse(s as String)", the call is supplying an arglist of type "(String?)". (printinput)

Re: How to ADD inputs in a program

PostPosted: Sun Feb 08, 2015 9:42 am
by Charles
.readLine returns "String?" but .parse wants "String", in other words, nilable vs. non. You can change the code to:
x = Console.readLine ? ''

... in other words nil coalesce with an empty string. This will guarantee non-nil.