MISC: Refactor coding contracts for type safety (#1653)

* MISC: Refactor coding contracts for type safety

This refactor does three things without any behavior changes:
* Adds type safety by introducing generic type parameters to the coding
  contract definitions, so they can use concrete types instead of
  "unknown". This also eliminates a bunch of boilerplate casts.
* Removes the unneeded CodingContractType class. We can use the metadata
  type directly.
* Introduces a new hidden state to coding contracts. Instead of
  generating and storing the data (which is what is shown to the user as
  the problem's input), the state is stored instead. This allows
  problems to (for instance) generate the answer up-front, and check the
  solution directly against the answer, instead of needing to embed a
  solver in the problem (which can then easily be ripped from the source
  code).

For compatibility, state == data by default. I plan to make use of this
feature in a followup, but it is unused currently.
This commit is contained in:
David Walker
2024-09-15 23:53:10 -07:00
committed by GitHub
parent cde5e3f6ae
commit 21ffa5322f
3 changed files with 158 additions and 256 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ export function CodingContractModal(): React.ReactElement {
const contractType = CodingContractTypes[contract.c.type];
const description = [];
for (const [i, value] of contractType.desc(contract.c.data).split("\n").entries())
for (const [i, value] of contractType.desc(contract.c.getData()).split("\n").entries())
description.push(<span key={i} dangerouslySetInnerHTML={{ __html: value + "<br />" }}></span>);
return (
<Modal open={contract !== null} onClose={close}>