Open cogumbreiro opened 1 year ago
This version of it with #{name : type}
annotations works:
(match-define (point #{a : Number} #{b : Number}) a)
(Edit: and by "works" I mean that it typechecks. it errors at runtime with a: undefined; cannot use before initialization
)
Renaming one of the 2 a
variables to something else also solves this issue, and allows it to run without the a: undefined; cannot use before initialization
error.
Renaming one of the 2
a
variables to something else also solves this issue, and allows it to run without thea: undefined; cannot use before initialization
error.
Ah, yes! Sorry, I forgot to add that the issue arises because a
appears in the match
and as a variable in a pattern. A workaround is naming variable a
in the pattern as a1
, for instance.
The problem has nothing to do with match-define. It's just that circular definitions require type annotations to break dependencies. For example, the program below has the same error.
(lambda ([a : Number] [b : Number])
(define a (point a b))
(void))
In the match
example,
(match a
[(point a b) b]
a
in the clause shadows a
being matched against.
Alternatively, you can use match-let
(lambda ([a : point])
(match-let ([(point a b) a])
b))
It's just that circular definitions require type annotations to break dependencies.
Can you please clarify why any of these examples are "circular"? I am not really sure I understand the use of circular in this context.
In my understanding, the pattern in the pattern-matching is introducing a new scope after the match-define
and within the match-let
, which would disambiguate any confusion (cycle?).
The program does not work like match-let
. Instead, the a
on the right hand side refers to the a
on the left hand side. If you put this program in DrRacket:
#lang racket
(struct point (x y))
(define (add a)
(match-define (point a b) a)
b)
you can see that the binding arrow from the RHS points to the LHS.
What version of Racket are you using?
8.2
What program did you run?
yields a typing error.
What should have happened?
I would expect no typing error, but maybe I'm misunderstanding the semantics of scoping with a
match-define
. For instance, the following example yields no errors.Alternatively, if some error is to be triggered, the existing error message is misleading.
If you got an error message, please include it here.