# Can this contract error be easily improved?

**URL:** <https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181>\
**Category:** General\
**Created:** [September 26, 2024, 7:29pm UTC](https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181 "2024-09-26T19:29:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [September 26, 2024, 7:29pm UTC](https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181/1 "2024-09-26T19:29:53Z")

</div>

Here's a program:

```racket
#lang racket

(define foo-contract
  (-> integer? day))

(struct day (a b c))

```

Note that I've omitted the intended `?` for the `day?` predicate in the contract, writing instead `day`. Oops.

The error it signals is

```scheme
day1: undefined;
 cannot reference an identifier before its definition

```

... with no error location.

In a program of this size, I can probably figure out what's going on here. In a larger program, it's hard. I got an error message about an undefined reference to `weekless-day2`, and I spent several minutes searching for instances of `weekless-day2` in my code before a bell went off in my head and I took a look at the contract part.

How hard would it be to get a source code location for this error message?

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [September 27, 2024, 7:42am UTC](https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181/2 "2024-09-27T07:42:45Z")

</div>

DrRacket, at least, highlights the `day` and shows the line number in the error backtrace.

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [September 27, 2024, 12:52pm UTC](https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181/3 "2024-09-27T12:52:29Z")

</div>

Racket Mode shows the error locations:

```scheme
————— run contract-error.rkt —————
day1: undefined;
 cannot reference an identifier before its definition
  in module: "/home/greg/src/racket/examples/contract-error.rkt"
  internal name: day1.1
Context (errortrace):
 /home/greg/src/racket/examples/contract-error.rkt:4:15 day
 /home/greg/src/racket/examples/contract-error.rkt:4:2 (-> integer? day)

```

Although not apparent from this text copy/paste, those are links and work with `M-x next-error`.

**Caveat** : That only happens when the (Emacs) variable `racket-error-context` is set to `'high` -- causing the program to be run using `errortrace`. (That `errortrace`-annotated version of your program will be slower, which isn't great for timing, but enables better error messages.)

Probably similar caveat for the "debugging" option in DrRacket (which IIRC is on by default).

You can also do this with `racket` command line, see docs.

---

<div class="post-metadata">

**Author:** ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)\
**Post date:** [September 27, 2024, 10:08pm UTC](https://racket.discourse.group/t/can-this-contract-error-be-easily-improved/3181/4 "2024-09-27T22:08:48Z")

</div>

Ah... okay, interesting. I see that I've been running with "No Debugging or Profiling". So I guess that's a big fat mea culpa, except for the fact that this program:

```racket
#lang racket
(+ 3 a)
(define a 9)

```

... does give me at least an approximate error location when running in "No Debugging or Profiling", and has an error backtrace as well.

Okay, I guess I learned something today...

Apologies for the noise.
