
Can #lang typed/racket
understand type boundaries in loops/recursion? For example:
#lang typed/racket
(struct sequence-thing ([store : Bytes] [size : Index]))
(: sequence-thing->list (-> sequence-thing (Listof Byte)))
(define (sequence-thing->list st)
(define show-stopper (sequence-thing-size st))
(let loop ([index : Index 0] [accumulator : (Listof Byte) null])
(if (< index show-stopper)
(loop (add1 index) ((inst cons Byte (Listof Byte)) (bytes-ref (sequence-thing-store st) index) accumulator))
accumulator)))
This doesn’t typecheck, because: type mismatch expected: Index given: Positive-Fixnum in: (add1 index)
Is there a way to make it see that (add1 index)
is of type Index
? Or am I misunderstanding something, perhaps?

The result of (add1 index)
is not always an Index
. See these words from the reference: > https://docs.racket-lang.org/ts-reference/type-ref.html?q=Index#%28form._%28%28lib._typed-racket%2Fbase-env%2Fbase-types..rkt%29._.Index%29%29\|Index is bounded by 0 and by the length of the longest possible Racket vector.

Instead of Index
, you can use Nonnegative-Fixnum
instead, and it should work

See https://github.com/racket/racket/pull/3396#issuecomment-694339663, and the below comment by yjqww6

We have our first macro contributed to the Syntax Parse Bee! There is a lot of buzz - who will be next? Maybe it will be you? See the announcement for details about the Syntax Parse Bee and how to join in:

This does indeed work, thanks! Reading yjqww6’s comment on GitHub though, I wasn’t quite able to understand whether this results in equivalent type constraints as it would if Index
was made to typecheck or if this relaxes type constraints somehow?

Aaaaaah yes… I think I understand now! Thanks again!

This is excellent. I’ve been looking for a way to feed back the knowledge I’ve gained on the syntax/parse library now I’ve mostly figured it out!

I know this is old, but updated: https://gist.github.com/benknoble/a01e20f09ee42fbfc6eca42e72200e84 I like the new syntax tremendously.

@spdegabrielle cloning the repository yields this error when I open syntax-parse-example.scrbl
. I get the same error when I run raco pkg install
or raco pkg setup

You don’t need to clone syntax-parse-example to contribute filling in the issue template on GitHub is all that is required

Ah I planned to make a PR instead. Very well. I’ll paste my code, tests & docs into an issue once it’s finished

The instructions are in the issue template; it asks for the following;
1. your macro
2. an example use of your macro
3. (optional) "before" code that your macro helps to improve
The first submission is a great example: https://github.com/syntax-objects/Summer2021/issues/2\|https://github.com/syntax-objects/Summer2021/issues/2

wonderful :)