1
votes

I'm following one of the exercises in the PureScript book where I have to define a function renderPath that takes an array of Points (x and y) and draws a path using them (using purescript-canvas), where a Point is defined as

type Point = { x :: Number, y :: Number }

I've decided to split the function into two steps. The first step is to moveTo the first point in the array and then use lineTo over the remaining points.

Here is the function I've written so far which only has the first step. But I get this one error that I just can't figure out.

Here's the function

import Prelude
import Control.Monad.Eff (Eff)
import Data.Array.Partial (head, tail)
import Data.Maybe (Maybe(..))
import Graphics.Canvas (CANVAS, Context2D, arc, closePath, fillPath, getCanvasElementById, getContext2D, lineTo, moveTo, rect, setStrokeStyle, strokePath)
import Partial.Unsafe (unsafePartial)

renderPath :: forall eff
. Context2D
-> Array Point
-> Eff (canvas :: CANVAS | eff) Unit

renderPath ctx points = fillPath ctx $ do
  p0 <- unsafePartial head points
  moveTo ctx p0.x p0.y

And I get this one error at p0 in the last line of the function (moveTo ctx p0.x p0.y)

The error is:

  Could not match type

    { x :: Number
    | t0         
    }            

  with type

    ( x :: Number
    , y :: Number
    )            


while checking that type ( x :: Number
                         , y :: Number
                         )            
  is at least as general as type { x :: Number
                                 | t0         
                                 }            
while checking that expression p0
  has type { x :: Number
           | t0         
           }            
while checking type of property accessor p0.x
in value declaration renderPath

where t0 is an unknown type

I'm assuming that p0 should be of the type { x :: Number, y :: Number }

This error shows one as { x :: Number | t0 } and the other as ( x :: Number, y :: Number). I don't understand this at all. Why is one using {} and the other (). What's the difference between them? Where am I going wrong?

I'd also request an explanation along with any correction as I'm new to PureScript and functional programming as a whole so I'm having trouble trying to understand a lot of concepts.

1

1 Answers

2
votes

I'm not entirely sure why you get that particular error message, but one issue with your code is this line:

p0 <- unsafePartial head points

this is supposed to be

let p0 = unsafePartial (head points)

<- in a do block means something like "get the value out of the context of Eff". But since unsafePartial (head points) is not an Eff, the types don't match and you get an error message.

this code compiles:

http://try.purescript.org/?backend=flare&gist=6baa84215d33bf6f11c808e24b95c72f