Scope modes · Coding Class
This topic is not available in the selected language or version. Showing the start of this section.
Apply the chapter in three steps: an exercise, a program project, and a debugging task. Implement first, then consult the hints and completion criteria. Use a separate file or working directory for each task.
1. Exercise · Enable references where needed
Section titled “1. Exercise · Enable references where needed”Run two versions of scope_modes.xen from the chapter: annotate all of main, then annotate only the call block.
Completion: Both print [1, 2, 3] and 10; identify the extent of reference permission in each.
Hint: Use #![explc] for a function and #scope[explc] for a block.
2. Build a program · Compare optimization modes
Section titled “2. Build a program · Compare optimization modes”Run the same program with —opt=off and —opt=basic and compare stdout. Also run basic with —opt-report, distinguishing the report from program output.
Completion: Outputs agree; the report appears on stderr. Output agreement alone is not evidence of a speed increase.
Hint: Use the chapter’s commands; do not make internal report identifiers part of expected output.
3. Debug · Fix a missing caller annotation
Section titled “3. Debug · Fix a missing caller annotation”Check the program below and fix it by enclosing only the reference-creating call in main in a block. Explain why —opt cannot fix it.
Start with this deliberately broken program.
#![explc]fn append(values: &mut Vec<Int>) { values.push(3); }fn main() { let mut values = [1, 2]; append(&mut values); println(values);}xen check class_scope.xenDiagnostic excerpt:
passing references requires #![explc]Completion: Output is [1, 2, 3]. Check the callee’s declaration and the caller’s scope separately.
Hint: A callee’s explc setting does not propagate to its caller.
Verify and record
Section titled “Verify and record”Use xen check file.xen followed by xen run file.xen for ordinary programs; use xen test file.xen for test-only files. Record the original error, its cause, and the corrected result, then rerun every input in the completion criteria.