cobalt
cobalt copied to clipboard
Reserved word for input
We currently have println
as a reserved word. Would it be useful to have a symmetric readln
?
Is this necessary? I mean, in Java the println
is just a method of PrintStream
that belongs to System
class.
Not necessary, I was just considering that (as far as I know) in languages where it is a keyword, such as Python, there is a symmetric input keyword, whereas in Java for example, it is a method call from System.(in|out)
, for both input and output.
Yeah, so probably we will need to decide which approach will be used - the IO as part of the language or IO as some out of language workaround. As you stated, now it seems like the first approach is partially implemented as the println
is reserved.
The only thing with this is it depends upon how often somebody would be using this feature. I'm assuming it would only be for console input? I haven't seen the python one.
It's definitely worth us thinking up ideas like this.
This also reminds me that we need to parse print
too.
Print was originally a statement in Python. In Java and Scala (if I understood correctly) it is just a regular function. This approach makes the language more "algorithmic" in sense that there is no direct in language reference to outside world I think.
What would be an example of usage for this? Would it be the equivalent of this:
final Scanner scanner = new Scanner(System.in);
final String input = scanner.nextLine();
Which in our code would be this?
val input = readLine
I can't seem to find an example of using this with Python so I feel I may have misunderstood.
Here is a pretty good extract of the reasons why the IO as statements (language features) might not be the best idea. (about Python)
I came up with a possibility how to make the language more algorithmic - but maybe the approach is too oldschool for current languages and lazy programmers who dont want to type :) Basically - the IO functions would be in some separate package you would import if you needed them. Otherwise you could use the function names how you want. While importing you could rename them too (other possible language feature?) The disadvantage is, that in tiny programs this adds boilerplate, because you need IO everywhere, when you have a program with single class. On the other hand, in bigger project, the IO namespace would not pollute the code of specialized classes. Also, if you are just writing some plugin class for project in other language, you just dont import it and everything is good.