Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

export from module

Here is a code taken from http://www.angelfire.com/tx4/cus/shapes/haskell98.html . It compiles and executes correctly in WinGHCi if I comment the names in the module header. But if the names are kept then it does not compile - it reports an error on the name MakeCircle. My question is: if I want to explicitly mention that I want to export MakeCircle, what code changes are required?

module Circle -- (Circle, MakeCircle, getRadius, setRadius)
  where
  import Shape

  class Shape a => Circle a where
      getRadius :: a -> Int
      setRadius :: a -> Int -> a

  instance Shape CircleInstance where
      getX = x
      getY = y
      setX a newx = a {x = newx}
      setY a newy = a {y = newy}
      moveTo a newx newy = a {x = newx, y = newy}
      rMoveTo a deltax deltay = a {x = ((getX a) + deltax), y = ((getY a) + deltay)}
      draw a =
         putStrLn ("Drawing a Circle at:(" ++ (show (getX a)) ++ "," ++ (show (getY a)) ++
            "), radius " ++ (show (getRadius a)))

   instance Circle CircleInstance where
      getRadius = radius
      setRadius a newradius = a {radius = newradius}

   data CircleInstance = MakeCircle {x, y, radius :: Int}
      deriving(Eq, Show)
like image 453
R71 Avatar asked Jan 24 '12 08:01

R71


People also ask

What is module export?

Module exports are the instruction that tells Node. js which bits of code (functions, objects, strings, etc.) to “export” from a given file so other files are allowed to access the exported code.

What can you export with module export?

By module. exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() method.

What is module exports in react?

2. Note that module.exports is a Node.js thing (or more accurately, a CommonJS thing) used to express what is exported from a module. I'm not sure you should be using it in a React/ES6 environment. See es6 equivalent for module.exports.

How do I export a node js function?

The module.js is used to export any literal, function or object as a module. It is used to include JavaScript file into node. js applications. The module is similar to variable that is used to represent the current module and exports is an object that is exposed as a module.


2 Answers

MakeCircle is a data constructor for the type CircleInstance. Data constructors can only be exported in combination with their defining type. You will probably also want to export the Circle class methods getRadius and setRadius; with the current export list those methods will be unavailable outside this module.

Change your export list to

module Circle (Circle (..), CircleInstance (MakeCircle), getRadius, setRadius)

This shows two forms of exporting. The export Circle (..) exports the type class Circle and all of its methods, while CircleInstance (MakeCircle) exports the type constructor CircleInstance and only its data constructor MakeCircle. If you were to add a new data constructor for MakeCircle, perhaps a UnitCircle, that constructor wouldn't be exported from the module unless you either mention it in the export list (i.e. CircleInstance (MakeCircle, UnitCircle) ) or use the (..) form of exports.

like image 115
John L Avatar answered Nov 07 '22 05:11

John L


The export list is incorrect, it should be:

module Circle(Circle, CircleInstance(MakeCircle), getRadius, setRadius)

Or perhaps

module Circle(Circle, CircleInstance(..), getRadius, setRadius)
like image 39
Chris Kuklewicz Avatar answered Nov 07 '22 05:11

Chris Kuklewicz