Language comparison cheat sheet
Use these tables to look up familiar syntax. For explanations and runnable examples, read Toit for programmers or the guides for Python, C/C++, and JavaScript.
The entries compare common tasks, not identical semantics. In particular,
Toit annotations check values at runtime, only false and null are falsy,
and a block can return from its enclosing function. Code fragments assume
any required imports and enclosing declarations.
| Toit | Python | |
| Current object | this | self |
| Single line comments | // | # |
| Logical 'and', 'or' and 'not' operators | and or not | and or not |
| Shift left, right, unsigned right | << >> >>> | << >> (no unsigned shift) |
| Integer division | / (on integer types) | // |
| Integer sizes | 64 | (arbitrary) |
| Statement grouping | (indent) | (indent) |
| Define a class Foo that inherits from Bar | class Foo extends Bar: | class Foo(Bar): |
| Define constructor for class Foo | constructor x: | def __init__(self, x): |
| Define constructor for class Foo that calls constructor of superclass Bar | constructor x: super x | def __init__(self, x): super().__init__(x) |
| Constructor that assigns to fields | constructor .x: | def __init__(self, x): self.x = x |
| Check object has type | bar is Foo | isinstance(bar, Foo) |
| Check object does not have type | bar is not Foo | not isinstance(bar, Foo) |
| Call a method foo with two arguments | foo x y | foo(x, y) |
| Declare a member variable in a class | x := null x := ? x/int := 0 x/int := ? | self.x = None # Inside a method. |
| Declare a local variable in a method | x := null x := ? x/int := 0 x/int := ? | x = None |
| Define a constant | X ::= 0 | X = 0 # By convention. |
| Define a constant in a class | static X ::= 0 | X = 0 # Class attribute, by convention. |
| Define a top-level function | foo x y: | def foo(x, y): |
| Define an instance method in a class | foo x y: | def foo(self, x, y): |
| Define a static method in a class | static foo x y: | @staticmethod def foo(x, y): |
| If statement | if condition: | if condition: |
| Fixed loop | end.repeat: |i| | for i in range(end): |
| Three-part for loop | for i := 0; i < end; i++: | Use range or a while loop. |
| Iterate over collection | collection.do: |x| | for x in collection: |
| While loop | while condition: | while condition: |
| Import local from library | import .library | from . import library # Within a package. |
| Import from library | import library | import library |
| Import into current namespace | import library show * | from library import * |
| Print/log | print "Hello" | print("Hello") |
| Print with interpolation | print "Hello $name" | print(f"Hello {name}") |
| Interpolate with padding | print "Hello $(%9s name)" | print(f"Hello {name:>9}") |
A final Toit binding does not freeze its object. Python constants are a naming
convention; JavaScript class fields are not made final by static.
C++ type tests above assume a polymorphic base and a pointer. Toit object
references are managed values, and is does not require a pointer cast.
C/C++ #include is textual inclusion; Toit imports load library declarations.
Python // floors the result; Toit integer / truncates toward zero. JavaScript
Number division is floating point, and its bitwise operators use 32-bit values;
Toit integers are signed 64-bit values. The tables show the closest common
spelling, not a promise that porting the expression preserves every edge case.