In this tutorial we will learn how to create an object, read console and make simple operation with numbers.
First of all let’s create a variable and assigns a newly created object to it. To create an object we have to send a message "new" to its class:
To read the console we have to send "readLine" to the console symbol
The message will return the entered text. Now we have to convert it to the number. To do so we could use an extension – extensions'convertorOp
The extension method "convertTo" overrides a literal and converts its value to an integer.
Then we will do the same for the second number:
Now let’s sum two numbers, print the result and wait for any key
Our code works well if the user will enter a correct number, in all other cases the process will be broken. To warn the user if the number is wrong we have to catch an exception:
So far so good. Now we could refactor our code a little. To do so we have to declare a helper extension EReader which will read and convert the number from the console
So our code will look like this
How it works? EReader temporary overrides the variable but self still points to the original class.
Now let’s put all together