racket / racket/htdp

Support type-annotated `struct` definitions

Open
#259 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

signature
Dominant language
Racket
Stars
112
Forks
73
Avg merge
9h 41m
Merged PRs (30d)
1

Description

When one defines a type based on a struct, one often wants to
constrain the types of fields. In the student languages' signature
(type) annotations, this can be achieved by providing a signature for
the struct's constructor:

(define-struct name [first last])

(: make-name (String String -> (NameOf String String)))

While this works, Name would still refer to a struct with two
fields of any type, whereas the type NameOf String String denotes
the "constrained" type with two strings as fields.

Another issue is that predicate name? would still accept any
instance of name, whereas it should only return #true for
instances with fields of the right types.

It would be convenient if one could add type constraints to the fields
of a define-struct form, or anyway having a mechanism to easily
define a struct with certain type constraints on its fields.

For example something like:

(define-struct name [(: first String) (: last String)])

Such a definition would also define the following type definitions and
constraints:

  • Name would be a signature form equivalent to what is currently called NameOf String String
  • make-name would be given the signature String String -> (NameOf String String)
  • name? would return #true iff its argument is an instance of name with both fields that are Strings
  • name-first and name-last would have the signature (NameOf String String) -> String

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing the existing student-language signature handling for define-struct and NameOf, including how make-name, the predicate, and accessors are specified. Done means agreeing on the syntax and semantics for constrained fields, then covering the constructor, predicate, and accessor behavior described in the issue with tests.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.