Reading input: prompt

prompt shows a text, waits until the user types a line and presses Enter, and gives back that line:

name: str = prompt("Enter your name: ")
print("Your name is ${name}")

In a terminal, a run looks like this (the user typed Ann):

Enter your name: Ann
Your name is Ann

The text is shown as it is, without a new line after it, so the user types right after it. That’s why it usually ends with a space. prompt() without a text just waits for a line.

Input from a file or another program

A program reads its input from the terminal, or from wherever it is sent:

luma run greet.luma < answers.txt
echo Ann | ./greet

Each prompt then reads the next line. The lines don’t appear on screen, because nobody types them, so the text of a prompt and the next output end up on the same line. The examples on this page show runs like that:

name: str = prompt("Enter your name: ")
print("Your name is ${name}")

Input:

Ann

Output:

Enter your name: Your name is Ann

The type decides how the line is read

The line is read as the type of the variable it goes into:

age: int = prompt("Age: ")
price: float = prompt("Price: ")
small: byte = prompt("Byte: ")
save: bool = prompt("Save? ")
print(age + 1)
print(price * 2.0)
print(small)
print(save)

Input:

41
2.5
200
true

Output:

Age: Price: Byte: Save? 42
5.0
200
true
TypeWhat the user may type
stranything; the line is kept exactly as typed, spaces included
inta whole number: 42, -7, +3, 007
bytea whole number from 0 to 255
floata number like 2.5, -0.25, 3, .5, 1e3, 2.5E-3
booltrue or false

For numbers and bools, spaces and tabs around the text are ignored: 42 is 42. Anything else is invalid input (see below).

The same rule works wherever a value of a known type is expected, for example when adding to a list or a map:

names: [str] = []
names[] = prompt("Name: ")
scores: {str: int} = {}
scores["Ann"] = prompt("Ann's score: ")
print(names)
print(scores)

Input:

Bo
17

Output:

Name: Ann's score: ["Bo"]
{"Ann": 17}

Where no particular type is expected, the line is a str, for example in print(prompt()) or "Hi ${prompt()}". To use a number in a calculation, read it into a variable first: in prompt() + 1, the prompt() is a str.

Waiting for Enter

On its own, prompt waits for a line and throws it away:

prompt("Press Enter to continue...")
print("Let's go!")

Input:

Output:

Press Enter to continue...Let's go!

Invalid input

If the line doesn’t fit the type, prompt raises error 6. Without a try, the program stops:

age: int = prompt("Age: ")
print(age)

Input:

forty

Output:

Age: example.luma:1:12: runtime error: invalid input "forty": expected an int

With try, the program can ask again. A walk limits how many times:

age: int = 0
err: error = nil
[1, 2, 3].walk(attempt) {
    try err {
        age = prompt("Age: ")
    }
    if err == nil { stop }
    print("Please type a number.")
}
print(age)

Input:

forty
40

Output:

Age: Please type a number.
Age: 40

End of input

When there is nothing left to read, prompt raises error 7, end of input. This happens at the end of a file sent to the program, or when the user presses Ctrl-D (Linux) or Ctrl-Z then Enter (Windows). A last line that doesn’t end with a new line still counts as a line.

err: error = nil
try err {
    line: str = prompt()
}
print(err)

Output:

error 7: end of input

Details

  • A line ends at a new line character, which isn’t part of it. A \r just before it (Windows line endings) is dropped too.
  • A float is read as the float nearest to the number typed, however many digits it has, exactly like a float written in a program: every float Luma prints reads back as the same float.
  • A number too large for its type is invalid input: 9223372036854775808 for an int, 256 for a byte, 1e999 for a float.

Not available yet

  • A default answer, a time limit, or a check function for prompt (the original Luma had these). Use try and if instead.

See Limitations.