Search code examples
purescriptnewtype

Importing Newtype(s) from another module


I'm learning PureScript lately and did a small application that draws a cube on the screen. All is well and I had a few newtypes defined in the top of the Main module as follows:

newtype Vec2 = Vec2
    {
        x :: Number
    ,   y :: Number
    }

newtype Vec3 = Vec3
    {
        x :: Number
    ,   y :: Number
    ,   z :: Number
    }

I also got some functions that does transformations such as perspectiveDivide etc., that I want to move to another module Transforms just to organize better. So I moved these types to the Transforms module, and exported them.

module Transforms (Vec2, Vec3, perspectiveDivide) where

and the above types follows this.

Now I tried to import these in the main module with the selective import feature, but it didn't work.

import Transforms (Vec2, Vec3, perspectiveDivide)

but it still gave me an error when trying to use a function projectToScreen which was still in the main module. The error is like this:

in module Main at src/Main.purs line 30, column 10 - line 30, column 18

Unknown data constructor Vec3

See https://github.com/purescript/documentation/blob/master/errors/UnknownName.md for more information, or to contribute content related to this error.

I'm both new to functional programming and also PureScript. What does this error mean?


Solution

  • module Transforms (Vec2, Vec3, perspectiveDivide) where only exports the type constructors.

    You want to use Vec2(..) to export the data constructors as well. The same syntax works for the imports.