Reputation: 2063
I know the stdin.read_line() function, but I wanted to make my code less verbose via the use or something more in line to raw_input() in python.
So I found out about GNU ReadLine in this discussion about vala, however I can`t reproduce it in Genie.
The python code that I want to mimic is:
loop = 1
while loop == 1:
response = raw_input("Enter something or 'quit' to end => ")
if response == 'quit':
print 'quitting'
loop = 0
else:
print 'You typed %s' % response
The far I could get was:
[indent=4]
init
var loop = 1
while loop == 1
// print "Enter something or 'quit' to end => "
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
if response == "quit"
print "quitting"
loop = 0
else
print "You typed %s", response
And tried to compile with:
valac --pkg readline -X -lreadline loopwenquiry.gs
But I am getting the error:
loopwenquiry.gs:7.24-7.31: error: The name `ReadLine' does not exist in the context of `main'
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
^^^^^^^^
loopwenquiry.gs:7.22-7.81: error: var declaration not allowed with non-typed initializer
var response = ReadLine.read_line("Enter something or 'quit' to end => ")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
loopwenquiry.gs:8.12-8.19: error: The name `response' does not exist in the context of `main'
if response == "quit"
^^^^^^^^
loopwenquiry.gs:12.35-12.42: error: The name `response' does not exist in the context of `main'
print "You typed %s", response
^^^^^^^^
Compilation failed: 4 error(s), 0 warning(s)
What am I doing wrong?
Thanks.
Upvotes: 0
Views: 107
Reputation: 4289
As stated in the comment from Jens the namespace is Readline, not ReadLine. The function is also readline, not read_line. So your working code would be:
[indent=4]
init
while true
response:string = Readline.readline("Enter something or 'quit' to end => ")
if response == "quit"
print "quitting"
break
else
print "You typed %s", response
I notice you use valac --pkg readline -X -lreadline loopwenquiry.gs
to compile, which is good. The -X -lreadline
tells the linker to use the readline
library. In most cases you won't need to do this because there is a pkg-config file, these have a .pc
file extension, that contains all the necessary information. It looks as though someone has submitted a patch to fix this to the readline library. So using -X -llibrary_i_am_using
should be the exception as most libraries have a .pc
file.
I have also used while..break
for the infinite loop to see if you think that is a slightly clearer style.
Upvotes: 1