Assignability and representability
Whether a value can be assigned is a spec question; whether a constant fits is a representability question.
Canonical guidance
- assignability is narrower than “looks compatible”
- representability matters especially for constants assigned to numeric types
- identical or assignable types do not erase the need for explicit conversions in many cases
Use when
- checking assignment and return statements
- reviewing numeric constants
- diagnosing interface or generic type mismatches
Avoid
- assuming same underlying representation implies assignability
- ignoring constant range limits
- blurring assignability with convertibility
Preferred pattern
const port = 8080
var p uint16 = port
Anti-pattern
- assuming a large untyped integer constant will fit any integer target type
Explanation: This is tempting because constants feel abstract, but representability is checked against the destination type at compile time.
Why
- many “cannot use X as Y” errors reduce to assignability or representability, not to broader API design issues