Error Lab
Every diagnostic below is the real output of ptc, not a mock-up. Pick a mistake, read how the compiler teaches the canonical form, then flip to the fix.
A bound an importer can name
Since 0.42 a pub function may be bounded by any trait an importer can spell — a built-in, the module's own pub trait, an imported one. A private trait is the one thing nobody outside can name, so no type of theirs could ever satisfy the bound; the error names both ways out.
trait Scored:
/// The score.
fn score(self) -> Int
/// The best of the values.
pub fn best[T: Scored](xs: List[T]) -> Int:
mut top = 0
for x in xs:
if x.score() > top:
top = x.score()
return topThe compiler answers
error: 'best' is bounded by 'Scored', a trait this module keeps private — an importer could never name it, so no type of theirs could satisfy the bound. Declare 'pub trait Scored:' (an importer then spells it module-qualified, like every imported name), or drop 'pub' from the function (line 5, column 1)/// A scoring contract.
pub trait Scored:
/// The score.
fn score(self) -> Int
/// The best of the values.
pub fn best[T: Scored](xs: List[T]) -> Int:
mut top = 0
for x in xs:
if x.score() > top:
top = x.score()
return top
record Card:
points: Int
with Scored:
fn score(self) -> Int = self.points
fn main() -> Void:
print("{best([Card(points: 3), Card(points: 8)])}")Fixed — and it runs
8