2
votes

Having a problem with some of the examples in the Learn Purescript by Example book. Specifically this code from section 9.3:

main :: Eff (canvas :: CANVAS) Unit
main = void $ unsafePartial do
  Just canvas <- getCanvasElementById "canvas"
  ctx <- getContext2D canvas

  setFillStyle "#0000FF" ctx -- this's line 16 referred to in the error message

  fillPath ctx $ rect ctx
    { x: 250.0
    , y: 250.0
    , w: 100.0
    , h: 100.0
    }

gives the following error:

in module Example.Rectangle at src\Example\Rectangle.purs line 16, column 3 - line 16, column 29

A result of type

Context2D

was implicitly discarded in a do notation block. You can use _ <- ... to explicitly discard the result.

while applying a function discard of type Discard t0 => Bind t1 => t1 t0 -> (t0 -> t1 t2) -> t1 t2 to argument (setFillStyle "#0000FF") ctx while inferring the type of discard ((setFillStyle "#0000FF") ctx) in value declaration main

where t0 is an unknown type t2 is an unknown type t1 is an unknown type

See https://github.com/purescript/documentation/blob/master/errors/NoInstanceFound.md for more information,

The suggested error doesn't help and I can't work out what "discard" acctualy does. I also noticed a similar problem with, for example, the "simulate" function from section 8.17. If I try the suggestion of assigning using "_ <- " more random seeming errors crop up.

(This is using PSCi 0.11.5)

1
Please, share your code, not link to book. This don't work.timiTao

1 Answers

3
votes

It is no more allowed to implicitly discard a value in a do block.

You can: - ignore the value explicitly: _ <- setFillStyle.... - or if the return value is Unit (e.g. Eff fx Unit) you can simply import "discard" from "Prelude"