Records
ASPL 0.7.0 · Current release
Construct named question fields, share a type and change an independent copy.
On this page
What you will learn
Define a question record with text and an integer answer, construct two values of that type, keep them in a typed list, and change an independent copy.
Before you begin
Read functions, strings and lists. This independent example includes its own entry. Open it as a new Home draft, then choose Run explicitly. Earlier drafts remain available.
Give a question named fields
A file-scope record / سجل declaration defines the named type Question. Its fields are prompt: string / سؤال_نصي: نص and answer: i64 / جواب: ص64, separated by a comma. The prompt is text; the answer is an integer. The English source uses the user names Question, prompt and answer; the Arabic source uses سؤال, سؤال_نصي and جواب.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
record Question { prompt: string, answer: i64 }
fn start_main() -> i64 {
let first: Question = Question { prompt: "2 + 3?", answer: 5 };
let second: Question = Question { prompt: "3 * 4?", answer: 12 };
let questions: list<Question> = [first, second];
var copied: Question = questions[0];
copied::answer = 99;
print(questions[0]::prompt + "\n");
print(format_i64(questions[0]::answer) + "\n");
print(questions[1]::prompt + "\n");
print(format_i64(questions[1]::answer) + "\n");
print(format_i64(first::answer) + "\n");
print(format_i64(copied::answer) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "2 + 3?\n5\n3 * 4?\n12\n5\n99\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "2 + 3?\n5\n3 * 4?\n12\n5\n99\n"
}
}Question { prompt: "2 + 3?", answer: 5 } constructs a value, supplying every field exactly once. first and second share the same type while storing different prompts and answers. list<Question> / قائمة<سؤال> puts both values in questions. The indexes 0 and 1 select the first and second question. Postfix :: reads a named field, so questions[0]::prompt is text and questions[0]::answer is an integer.
print / اطبع accepts text. Print the prompt directly, but explicitly use format_i64 / نسق_ص64 for the answer; each line adds \n. var copied: Question = questions[0] / متغير منسوخ: سؤال = أسئلة[0] copies the selected question into a mutable root. Assigning copied::answer = 99 changes only that copy. Parameters, let roots and temporary values cannot be mutated. The entry returns normal status 0, separate from the answer values.
Predict the independent values
Before Run, predict first::answer after the copied field changes. Exact output is 2 + 3?\n5\n3 * 4?\n12\n5\n99\n, with status 0. The original first question still answers 5; the second still answers 12; the copy answers 99. Constructing a list and reading an element copy values too. Changing a nested list or record in a copy also leaves the original unchanged.
Two separately declared record types remain different types even with identical fields. Missing, repeated or unknown fields and a field of the wrong type are source errors. They prevent execution; they do not return a failed result. See the records reference.
Change the copied answer
Change only copied::answer = 99 to copied::answer = 42. Expected output is exactly 2 + 3?\n5\n3 * 4?\n12\n5\n42\n, with normal status 0.
Set the copy's answer to 42. Predict which printed lines change.
Hint: Change the integer assigned through copied. Keep both constructors and questions unchanged.
Show worked answer
Only the last line changes to 42. The two questions in the list still contain their original prompt and answer fields, and first still has answer 5. copied shares their Question type but holds an independent value. Text prints directly; each numeric field still needs explicit formatting. The entry returns normal status 0.
Execution: Browser and native · ASPL 0.7.0
- Read the example explanation in its lesson before running.
Input: No input lines are required.
record Question { prompt: string, answer: i64 }
fn start_main() -> i64 {
let first: Question = Question { prompt: "2 + 3?", answer: 5 };
let second: Question = Question { prompt: "3 * 4?", answer: 12 };
let questions: list<Question> = [first, second];
var copied: Question = questions[0];
copied::answer = 42;
print(questions[0]::prompt + "\n");
print(format_i64(questions[0]::answer) + "\n");
print(questions[1]::prompt + "\n");
print(format_i64(questions[1]::answer) + "\n");
print(format_i64(first::answer) + "\n");
print(format_i64(copied::answer) + "\n");
return 0;
}
Expected outcomes
{
"browser": {
"check": "success",
"class": "success",
"output": "2 + 3?\n5\n3 * 4?\n12\n5\n42\n",
"processStatus": 0
},
"native": {
"check": 0,
"status": 0,
"output": "2 + 3?\n5\n3 * 4?\n12\n5\n42\n"
}
}Check your understanding
Explain the difference between a record type and a record value. Identify which fields need formatting and why changing a copy does not update the list. No grade or progress is stored, and navigation stays open. Return to the Tutorial path or continue to the recoverable results reference.