[racket] struct change question.

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Sat Apr 14 08:07:40 EDT 2012

On Apr 14, 2012, at 5:16 AM, Roelof Wobben wrote:

> So I thought I could use something like this :
> ; Editor(e)  Keyevent (k) -> Editor
> ; Change the Editor according to the keyevent
> (define (edit e k)
> (cond
>  [ (key=? k "left") (string-append (editor-pre e) (substring (editor-post e) 1 1))))
> But now I get a message on my display function that it's expecting a Editor but given "aaa"

> My question is now How can i change the "aaa" and "bbb" part and still returning a Editor?
> Do I need to use make-editor all the time ?

In a word, yes.  Your contract for "edit" says it returns an editor, but your actual definition returns a string instead; you're violating the contract.

BTW, I recommend adding a "check-with" clause:
(big-bang verwerker
                   (check-with editor?)
                   (on-key edit)
                   (on-draw <whatever>)
                   <other clauses you were already using>
This causes the animation to check immediately after every event to make sure the "world" is still an editor, and if it isn't, you'll get an informative error message telling you which function violated its contract.

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.