In this post I am going to explain, how
to import types, name constructor, value constructors defined in one module to
other modules.
For example, My CustomTypes.hs defined
some types like below.
CustomTypes.hs
module CustomTypes () where type FirstName = String type LastName = String type EmpId = Integer type NoOfReportees = Integer {- Define an employee type -} data Employee = Engineer FirstName LastName EmpId | Manager FirstName LastName EmpId NoOfReportees | Director FirstName LastName EmpId NoOfReportees deriving (Show)
Define Test.hs
like below.
Test.hs
import CustomTypes emp1 = Engineer "Hari Krishna" "Gurram" 123
Try to load
Test.hs file.
Prelude> :load Test.hs [1 of 2] Compiling CustomTypes ( CustomTypes.hs, interpreted ) [2 of 2] Compiling Main ( Test.hs, interpreted ) Test.hs:3:8: Not in scope: data constructor ‘Engineer’ Failed, modules loaded: CustomTypes.
Observe above snippet, Haskell clearly
telling that Data constructor Engineer is not available in Test.hs file.
Update the CustomTypes.hs file by using
following statement.
module CustomTypes (Employee(Engineer))
where
Above statement clearly tells to Haskell
that, Data constructor Employee; value constructor Engineer is available to use in outside the world.
CustomTypes.hs
module CustomTypes (Employee(Engineer)) where type FirstName = String type LastName = String type EmpId = Integer type NoOfReportees = Integer {- Define an employee type -} data Employee = Engineer FirstName LastName EmpId | Manager FirstName LastName EmpId NoOfReportees | Director FirstName LastName EmpId NoOfReportees deriving (Show)
Reload Test.hs.
*Main> :load Test.hs [1 of 2] Compiling CustomTypes ( CustomTypes.hs, interpreted ) [2 of 2] Compiling Main ( Test.hs, interpreted ) Ok, modules loaded: CustomTypes, Main. *Main> *Main> emp1 Engineer "Hari Krishna" "Gurram" 123 *Main>
Now update
Test.hs like below and reload.
Test.hs
import CustomTypes emp1 = Engineer "Hari Krishna" "Gurram" 123 manager1 = Manager "Anand" "Bandaru" 234 5
*Main> :load Test.hs [1 of 2] Compiling CustomTypes ( CustomTypes.hs, interpreted ) [2 of 2] Compiling Main ( Test.hs, interpreted ) Test.hs:4:12: Not in scope: data constructor ‘Manager’ Perhaps you meant variable ‘manager1’ (line 4) Failed, modules loaded: CustomTypes. *CustomTypes>
Above snippet clearly telling data
constructor Manager is not available in Test.hs file, to solve the above error
we need to update the module statement in CustomTypes.hs like below.
module
CustomTypes (Employee(Engineer, Manager)) where
CustomTypes.hs
module CustomTypes (Employee(Engineer, Manager)) where type FirstName = String type LastName = String type EmpId = Integer type NoOfReportees = Integer {- Define an employee type -} data Employee = Engineer FirstName LastName EmpId | Manager FirstName LastName EmpId NoOfReportees | Director FirstName LastName EmpId NoOfReportees deriving (Show)
*CustomTypes> :load Test.hs [1 of 2] Compiling CustomTypes ( CustomTypes.hs, interpreted ) [2 of 2] Compiling Main ( Test.hs, interpreted ) Ok, modules loaded: CustomTypes, Main. *Main> *Main> emp1 Engineer "Hari Krishna" "Gurram" 123 *Main> *Main> manager1 Manager "Anand" "Bandaru" 234 5 *Main>
How
can I expose all the value constructors to outside world?
‘Employee(..)’ expose the type and all
the constructors of it.
CustomTypes.hs
module CustomTypes (Employee(..)) where type FirstName = String type LastName = String type EmpId = Integer type NoOfReportees = Integer {- Define an employee type -} data Employee = Engineer FirstName LastName EmpId | Manager FirstName LastName EmpId NoOfReportees | Director FirstName LastName EmpId NoOfReportees deriving (Show)
Test.hs
import CustomTypes emp1 = Engineer "Hari Krishna" "Gurram" 123 manager1 = Manager "Anand" "Bandaru" 234 5 director1 = Director "Sailaja" "PTR" 345 53
*Main> :load Test.hs [1 of 2] Compiling CustomTypes ( CustomTypes.hs, interpreted ) [2 of 2] Compiling Main ( Test.hs, interpreted ) Ok, modules loaded: CustomTypes, Main. *Main> *Main> emp1 Engineer "Hari Krishna" "Gurram" 123 *Main> *Main> manager1 Manager "Anand" "Bandaru" 234 5 *Main> *Main> director1 Director "Sailaja" "PTR" 345 53 *Main>
No comments:
Post a Comment