Terminology (5)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Type inference
- The checker working out a type the code did not write, from the value or the use.
- Coercion
- A conversion the language makes on its own, without a conversion in the code.
- Conversion
- Code that turns a value of one type into another, such as Number(text), float64(n) or as f64.
- Truncation
- Dropping the fraction of a number, rounding toward zero.
- Unsigned
- An integer type whose representable values are nonnegative. Out-of-range arithmetic handling depends on the language, operation and checked or wrapping mode.
Choosing a type and converting a value
Type inference chooses a type from an expression and its context when no annotation is written. That chosen type still governs the operations that follow. Converting a result afterward is a different step from converting an operand before the operation.
Coercion is a conversion the language applies implicitly; an explicit conversion is one the program requests. Neither name establishes that information is preserved. Inspect the source and destination types, the supported range and the operation’s treatment of fractions, signs and failed text conversion.
01Inferred types
An inferred type decides how the arithmetic is done
A film club averages runtimes, counts down to screenings and totals ticket sales. Each calculation passes through types nobody wrote: an inferred integer, a string read from a cue sheet, a price that becomes cents. Each language decides differently when a value is converted and what is lost.
Before summarizing the film programme, reduce the average to 7 total minutes across 2 short films. total and films have no written type: predict their quotient.
package main
import "fmt"
func main() {
total := 7
films := 2
fmt.Println(total / films)
}
go run main.go
Use the selected native command. The C++ track uses 32-bit int and unsigned; the Rust command selects edition 2024 so TryFrom is in the prelude.
02Implicit conversions
Some languages convert on their own, and some refuse
The screening tools also accept textual fields, unsigned countdown data or bounded seat counts. These reduced probes test what the language converts before the calculation or comparison. Predict the selected native result.
package main
import "fmt"
func main() {
var ratio float64 = 7 / 2
var exact float64 = 7.0 / 2
fmt.Println(ratio, exact)
}
03Explicit conversions
An explicit conversion still has to decide what to do with a value that does not fit
A report needs a deliberate conversion, but writing a conversion does not specify every failure or boundary policy. Inspect the shown text and numeric conversions before using the same tools for ticket amounts.
package main
import (
"fmt"
"strconv"
)
func main() {
code := 65
price := 3.9
fmt.Println(string(rune(code)), strconv.Itoa(code), int(price), int(-price))
}
04Repair the film club
Runtimes, the cue sheet, then ticket totals
The film-club toolkit needs a runtime summary for programme planning, cue-sheet status for screenings, and a ticket-sales report for reconciliation. Repair the complete native utility supplied for each job.
Each file contains its imports, reader and complete caller.
Repair the film club
Summarize numeric runtimes, interpret cue-sheet times, then convert and display signed ticket totals.
In the frontend Convert a runtime field before adding the trailer minutes. Optional
A schedule form passes runtime text to a preview that adds a five-minute trailer. The preview must interpret that field as a number before addition; string concatenation would display a different duration. Empty or non-finite input gets a prompt instead of a numeric preview.
export default function RuntimePreview({ minutesText }: { minutesText: string }) {
const minutes = Number(minutesText);
if (minutesText.trim() === "" || !Number.isFinite(minutes)) {
return <p>Enter runtime minutes</p>;
}
return <p>With trailer: {minutes + 5} minutes</p>;
}
Each render computes from current props. Number is an explicit conversion; the empty-text check matters because Number of empty text is zero. This preview does not validate the film-club import’s full range or format.
Sources: ECMAScript: Number conversion,React: keeping components pure,Svelte: $derived.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?