Appendix D. Coming from Python, JavaScript or Java
This appendix does not reteach the language. It maps the habits you already have onto Sprig, one line at a time: on the left is what you are used to, on the right is what Sprig writes. Every Sprig fragment has run on the compiler; each section ends with a complete program that brings the most common differences together.
If you can already program, you can skim Chapters 1–4, but read Chapter 3 (numbers), Chapter 13 (values that may be missing) and Chapter 14 (errors) carefully — those are where Sprig differs from most languages.
D.1 Coming from Python
| Python habit | How Sprig writes it |
|---|---|
print(a, b) | print takes one value: print(a); build the text first |
1 / 2 gives 0.5 | Int / is rejected: 7.divTrunc(2) or 7.toFloatExact() / 2.toFloatExact() |
// floor division | a.divTrunc(b) |
-7 % 3 gives 2 | -7 % 3 gives -1: the sign of % follows the dividend (as in Java) |
if x: truthiness | the condition must be Bool: if x != 0:, if text != "": |
len(s), len(xs) | s.length() (String), xs.size() (collections) |
f-string f"n = {n}" | "n = " + n; + turns the right side into text |
None | null, which belongs only to T?; check != null before using it |
try/except | errors go into the signature: throws Error plus try/catch problem: Error: |
| lists are mutable by default | List is read-only and MutableList is mutable; xs[0] = v becomes xs.set(0, v) |
for i in range(n) | the same: for i in range(n): |
pass | the same: pass |
tuples (a, b) | no tuples: a one-line class class Pair(first: Int, second: Int) |
and, or, not | the same |
d.get(k) returns None | m.get(k) returns V?; m[k] is V? too, but m[k] += 1 requires the key to exist |
spr
# Coming from Python: f-strings, None, mutable lists, // and %.
# Sprig joins text with + and marks missing values with T?.
let cups = 3
let price = 2.5
let total = cups.toFloatExact() * price
print("total: " + total)
func find(parts: List[String], name: String) -> String?:
for part in parts:
if part == name:
return part
return null
let found = find(["bolt", "nut"], "nut")
if found != null:
print("found " + found)
else:
print("not found")
# Python lists are mutable by default; Sprig chooses MutableList explicitly.
let items: MutableList[String] = ["a", "b"]
items.append("c")
print(items)
# Python's // is divTrunc; % on negatives also differs from Python.
print(7.divTrunc(2))
print(-7 % 3)
print(7.toFloatExact() / 2.toFloatExact())text
total: 7.5
found nut
[a, b, c]
3
-1
3.5Line by line:
"total: " + totaljoins text and aFloat, with no f-string and nostr().findreturnsString?; afterif found != null:,foundis a non-null string inside the branch.itemsmust be declaredMutableList[String]before it canappend; withList[String],appendwould be rejected.7.divTrunc(2)is deliberate truncation;-7 % 3is-1because the sign follows the dividend.7.toFloatExact() / 2.toFloatExact()is the fractional division, and gives3.5.
D.2 Coming from JavaScript
| JavaScript habit | How Sprig writes it |
|---|---|
const / let | let binds once and var can be reassigned (the opposite of JS) |
=== / !== | == / !=, which are strict already: there are no implicit conversions |
undefined | does not exist; the only absence is null, and only inside T? |
template strings `n = ${n}` | "n = " + n |
array.length | xs.size() |
array.push(v) | xs.append(v) |
array.map(f) | xs.map(f); f is a typed lambda or a named function |
{} object literals | {"k": v} is a Map; for fixed fields use a one-line class |
x?.y, x ?? y | no optional chaining and no ??: write if x != null: first, or use or_else from @std/nulls |
async/await, Promises | virtual threads and tasks from @std/concurrent (Chapter 22) |
function f(x) {} | func f(x: Int) -> Int:, with a type on every parameter and the result |
Math.floor(x) | Float.floor(x); also sqrt, ceil and abs |
parseInt(s) | s.toIntOrNull() (returns null when missing) or s.toInt() (throws on failure) |
NaN === NaN is false | test with Float.isNaN(x); for approximate equality use x.approxEqual(y, tolerance) |
for (const x of xs) | for x in xs: |
| every number is a double | Int is a 64-bit integer and Float is binary64; the two never mix implicitly |
spr
# Coming from JavaScript: let/const, template strings, arrays and objects.
# Sprig's let binds once; var is the reassignable one.
var count = 0
let name = "Ada"
count += 1
print(name + " has " + count)
# JS's undefined does not exist; absence is T? plus null.
func lookup(ages: Map[String, Int], who: String) -> Int?:
return ages.get(who)
let maybe = lookup({"ada": 36}, "bob")
if maybe == null:
print("missing")
else:
print(maybe)
# A JS array's length is a property; Sprig uses size() and [] for elements.
let numbers = [3, 1, 2]
print(numbers.size())
print(numbers[0])
print(numbers.contains(2))
# A JS object literal is a Map here; read fields with get.
let person = {"name": "Grace", "city": "London"}
print(person.get("name"))
print("city" in person)text
Ada has 1
missing
3
3
true
Grace
trueLine by line:
var countcan takecount += 1; assigning tolet nameagain would reportSPR-NAME-LET-ASSIGN.lookupreturnsInt?and{"ada": 36}is aMap[String, Int]; when nothing is found,getreturnsnull.numbers.size()is 3 andnumbers[0]is 3; the array method issize(), not.length.person.get("name")reads a value out of the map, and"city" in persontests keys.
D.3 Coming from Java
| Java habit | How Sprig writes it |
|---|---|
public static void main(String[] args) | top-level statements are the program and run in source order; a function named main is not called automatically |
System.out.println(x) | print(x) |
final local variables | let; for a local that can be reassigned, var |
int overflow wraps around | Int checks overflow, and overflow is a runtime error |
integer / truncates | / is rejected: deliberate truncation is a.divTrunc(b) |
int is 32-bit, long is 64-bit | Int is 64-bit; write Int32 for 32 bits |
double, float | Float (binary64), Float32 |
a.equals(b) | == already compares by value (numbers, Bool, String, lists, maps, enums, variants) |
== on class objects compares references | the same: identity; equal fields do not make two objects equal |
| interfaces | contract classes (methods without bodies) plus conform (Chapter 16) |
extends | no inheritance; use contracts, composition or variants |
generics <T> | a generic T: block (Chapter 16) |
| checked exceptions | recoverable errors are throws Error or an error class you define; checked Java exceptions are checked too |
a Java method may return null | in Sprig that result is T?; you must check it before use |
| annotations, reflection | no annotations and no reflection-derived schemas; write explicit fields and functions |
int[] arrays | List[Int]; a Java array crosses as an opaque value (Appendix F) |
spr
# Coming from Java: main, int overflow, interfaces, integer division.
# Sprig's top-level statements are the program; main is not called automatically.
class Account:
let owner: String
var balance: Int = 0
func deposit(amount: Int) -> Unit:
balance += amount
let account = Account(owner="Ada")
account.deposit(100)
print(account.owner + ": " + account.balance)
# No inheritance: a contract class (methods without bodies) plus conform replaces interfaces.
class Sink:
func write(line: String) -> Unit
class Console:
func write(line: String) -> Unit:
print(line)
conform Console to Sink
let sink: Sink = Console()
sink.write("through the contract")
# Java's integer / truncates; Sprig rejects it and asks for intent.
print(7.divTrunc(2))text
Ada: 100
through the contract
3Line by line:
Account(owner="Ada")constructs the object with a named argument;depositchanges thevar balance.Sink's method has no body, soSinkis a contract class; afterconform Console to Sink, aConsolevalue can be used as aSink.7.divTrunc(2)is 3: integer division must state its "truncate" intent.- Java's
mainis not called automatically, so the whole program above is top-level statements from beginning to end.
Related chapters
- Chapter 3: Values, variables and arithmetic: the number families and explicit conversions.
- Chapter 4: Text: string methods and code points.
- Chapter 8: Lists and Chapter 9: Maps and sets:
List/MutableList,Map. - Chapter 11: Classes and objects: one-line classes, fields, methods.
- Chapter 13: Values that may be missing:
T?and narrowing. - Chapter 14: Handling errors:
throws,try/catch. - Chapter 16: Generics and contract classes:
generic, contracts andconform. - Chapter 22: Concurrency:
@std/concurrentin place of async/await.
