Skip to content

Commit

Permalink
better type params usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Jan 22, 2024
1 parent 9f2f5c0 commit 45e891d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 26 additions & 0 deletions src/typecheck/typecheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,32 @@ describe("custom types", () => {
});
});

test("allows using parametric types in constructors", () => {
const [types, errs] = tc(
`
type T<a, b> { C(b) }
let a = C
let b = C(1)
`,
);

expect(errs).toEqual([]);
expect(types).toEqual({
a: "Fn(t0) -> T<t1, t0>",
b: "T<t0, Int>",
});
});

test.todo("forbids unbound type params", () => {
const [types, errs] = tc(
`
type T { C(a) }
`,
);

expect(errs).not.toEqual([]);
});

test.todo("doesn't allow shadowing type params", () => {
const [types, errs] = tc(
`
Expand Down
7 changes: 4 additions & 3 deletions src/typecheck/typecheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function unboundTypeError<T extends SpanMeta>(
};
}

function castType(ast: TypeAst): Type {
function castType(ast: TypeAst): Type<Poly> {
switch (ast.type) {
case "named":
return { type: "named", name: ast.name, args: ast.args.map(castType) };
Expand All @@ -65,10 +65,11 @@ function castType(ast: TypeAst): Type {
args: ast.args.map(castType),
return: castType(ast.return),
};

case "var":
return { type: "quantified", id: ast.ident };

case "any":
throw new Error("TODO castType");
throw new Error("TODO invalid use of _");
}
}

Expand Down

0 comments on commit 45e891d

Please sign in to comment.