Episode 3 Tami's Suspicious Sum

Language Tour

In Episode 3 we met "3" + 4. What happens to it in other languages?

Momo

Here is one short program in each of Topaz, Python, JavaScript and Rust, all written to do the same thing.

Each one is written to print 시작 first (Korean for “start”), then the result of "3" + 4.

We followed the same steps twice with the tool and version shown in each box. The results matched.

  1. Topaz

    Run with the Topaz compiler 5.21.1

    Program main.tpz
    import std.io
    
    io.writeLine("시작")
    let result = "3" + 4
    io.writeLine("{result}")
    

    Commands

    1. Runtopaz run main.tpzExit status 1

      Standard output

      Empty

      Standard error
      error[TPZ5001]: `+` needs matching int, float, string, BigInt, or Decimal operands, found `string` and `int`
       --> main.tpz:4:14
        |
      4 | let result = "3" + 4
        |              ^^^^^^^
      
      main.tpz: 1 type diagnostic

    In plain words

    topaz run stopped at "3" + 4 on line 4. It reports TPZ5001, which says this + cannot take a string (string) and an integer (int) together.

    It ended with this error, so even the 시작 it was meant to print first was never printed.

    The last line of the error, 1 type diagnostic, marks it as an error found by the type check.

    Running topaz check on the same file gave the same error.

    This language’s rule

    Topaz checks the types on both sides of + before the program runs. To join text, both sides must be strings.

    Show the extra recordsHide the extra records
    1. Checktopaz check main.tpzExit status 1

      Standard output

      Empty

      Standard error
      error[TPZ5001]: `+` needs matching int, float, string, BigInt, or Decimal operands, found `string` and `int`
       --> main.tpz:4:14
        |
      4 | let result = "3" + 4
        |              ^^^^^^^
      
      main.tpz: 1 type diagnostic

    With "3" + "4" instead, which adds two strings, topaz run printed 시작 and 34 and finished.

    Program control-string-plus-string.tpz
    import std.io
    
    io.writeLine("시작")
    let result = "3" + "4"
    io.writeLine("{result}")
    
    1. Runtopaz run control-string-plus-string.tpzExit status 0

      Standard output
      시작
      34
      Standard error

      Empty

  2. Python

    Run with CPython 3.13.16

    Program main.py
    print("시작")
    result = "3" + 4
    print(result)
    

    Commands

    1. Runpython3.13 -u main.pyExit status 1

      Standard output
      시작
      Standard error
      Traceback (most recent call last):
        File "‹work folder›/main.py", line 2, in <module>
          result = "3" + 4
                   ~~~~^~~
      TypeError: can only concatenate str (not "int") to str

      ‹work folder› stands for the path of the folder that held the program. It differs from machine to machine, so it is hidden here.

    In plain words

    Python ran the first line and printed 시작.

    When it ran the addition on line 2, it raised a TypeError and stopped. A string (str) can only be joined to another string.

    The underline spans "3" + 4, with ^ directly under the + that raised the error.

    This language’s rule

    When Python runs this +, it needs another string to join to "3".

    Show the extra recordsHide the extra records

    Compiling the same file to bytecode with py_compile finished without an error. With this tool, the problem showed up only when the addition ran.

    1. Compilepython3.13 -m py_compile main.pyExit status 0

      Standard output

      Empty

      Standard error

      Empty

  3. JavaScript

    Run with Node.js 22.14.0

    Program main.js
    console.log("시작");
    const result = "3" + 4;
    console.log(result);
    

    Commands

    1. Runnode main.jsExit status 0

      Standard output
      시작
      34
      Standard error

      Empty

    In plain words

    node main.js printed 시작 and 34, then finished without an error.

    The number 4 was turned into a string and joined after "3".

    It prints as 34 without quotes, but the extra record asks with typeof and gets string. This "34" is not a fault. It is what the rule gives.

    This language’s rule

    When JavaScript joins a string and a number with +, it turns the number into a string and joins the two.

    Show the extra recordsHide the extra records

    node --check only reads the grammar and does not run the program. This program passed it.

    1. Checknode --check main.jsExit status 0

      Standard output

      Empty

      Standard error

      Empty

    typeof gives us string. The next line uses JSON.stringify to show the value with its quotes, "34".

    Program typeof-probe.js
    console.log("시작");
    const result = "3" + 4;
    console.log(typeof result);
    console.log(JSON.stringify(result));
    
    1. Runnode typeof-probe.jsExit status 0

      Standard output
      시작
      string
      "34"
      Standard error

      Empty

  4. Rust

    Compiled with rustc 1.96.0

    Program main.rs
    fn main() {
        println!("시작");
        let result = "3" + 4;
        println!("{result}");
    }
    

    Commands

    1. Compilerustc --edition 2024 main.rs -o mainExit status 1

      Standard output

      Empty

      Standard error
      error[E0369]: cannot add `{integer}` to `&str`
       --> main.rs:3:22
        |
      3 |     let result = "3" + 4;
        |                  --- ^ - {integer}
        |                  |
        |                  &str
      
      error: aborting due to 1 previous error
      
      For more information about this error, try `rustc --explain E0369`.
    2. RunNot run

      Folder after compiling main.rs

    In plain words

    rustc stopped while compiling. It reports E0369, which says an integer ({integer}) cannot be added to a &str.

    The error points at the + on line 3 and labels the type of each side.

    After compiling, the folder held only main.rs. No program file was made, so there was no run step, and 시작 was never printed.

    This language’s rule

    Rust checks at compile time whether + works with these two types.

Side by side

Look back at the four programs that try "3" + 4.

In which boxes was 시작 printed?

Which commands reported an error? Had the program started running when each one did?

What did the boxes that finished without an error print?