​

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.

ToitPython
Current objectthisself
Single line comments//#
Logical 'and', 'or' and 'not' operatorsand or notand or not
Shift left, right, unsigned right<< >> >>><< >> (no unsigned shift)
Integer division/ (on integer types)//
Integer sizes64(arbitrary)
Statement grouping(indent)(indent)
Define a class Foo that inherits from Barclass Foo extends Bar:class Foo(Bar):
Define constructor for class Fooconstructor x:def __init__(self, x):
Define constructor for class Foo that calls constructor of superclass Barconstructor x:
  super x
def __init__(self, x):
  super().__init__(x)
Constructor that assigns to fieldsconstructor .x:def __init__(self, x):
  self.x = x
Check object has typebar is Fooisinstance(bar, Foo)
Check object does not have typebar is not Foonot isinstance(bar, Foo)
Call a method foo with two argumentsfoo x yfoo(x, y)
Declare a member variable in a classx := null
x := ?
x/int := 0
x/int := ?
self.x = None # Inside a method.
Declare a local variable in a methodx := null
x := ?
x/int := 0
x/int := ?
x = None
Define a constantX ::= 0X = 0 # By convention.
Define a constant in a classstatic X ::= 0X = 0 # Class attribute, by convention.
Define a top-level functionfoo x y:def foo(x, y):
Define an instance method in a classfoo x y:def foo(self, x, y):
Define a static method in a classstatic foo x y:@staticmethod
def foo(x, y):
If statementif condition:if condition:
Fixed loopend.repeat: |i|for i in range(end):
Three-part for loopfor i := 0; i < end; i++:Use range or a while loop.
Iterate over collectioncollection.do: |x|for x in collection:
While loopwhile condition:while condition:
Import local from libraryimport .libraryfrom . import library # Within a package.
Import from libraryimport libraryimport library
Import into current namespaceimport library show *from library import *
Print/logprint "Hello"print("Hello")
Print with interpolationprint "Hello $name"print(f"Hello {name}")
Interpolate with paddingprint "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.