Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefit of importing specific parts of a Haskell module

Tags:

module

haskell

Except from potential name clashes -- which can be got around by other means -- is there any benefit to importing only the parts from a module that you need:

import SomeModule (x, y, z)

...verses just importing all of it, which is terser and easier to maintain:

import SomeModule

Would it make the binary smaller, for instance?

like image 554
Xophmeister Avatar asked Oct 23 '13 23:10

Xophmeister


People also ask

What is the use of import module?

Description. The Import-Module cmdlet adds one or more modules to the current session. Starting in PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module. However, you can still use the Import-Module command to import a module.

How do I import modules in Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

What does Module do in Haskell?

A module in Haskell serves the dual purpose of controlling name-spaces and creating abstract data types.

What is a qualified import?

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.


2 Answers

Name clashes and binary size optimization are just two of the benefits you can get. Indeed, it is a good practice to always identify what you want to get from the outside world of your code. So, whenever people look at your code they will know what exactly your code requesting.

This also gives you a very good chance to creat mocking solutions for test, since you can work through the list of imports and write mockings for them.

Unfortunately, in Haskell the type class instances are not that easy. They are imported implicitly and so can creates conflicts, also they may makes mocking harder, since there is no way to specify specific class instances only. Hopefully this can be fixed in future versions of Haskell.

UPDATE

The benifits I listed above (code maintenance and test mocking) are not limited to Haskell. Actually, it is also common practice in Java, as I know. In Java you can just import a single class, or even a single static variable/method. Unfortunately again, you still cannot selectively import member functions.

like image 194
Earth Engine Avatar answered Sep 22 '22 00:09

Earth Engine


No, it's only for the purpose of preventing name clashes. The other mechanism for preventing name clashes - namely import qualified - results in more verbose (less readable) code.

It wouldn't make the binary smaller - consider that functions in a given module all reference each other, usually, so they need to be compiled together.

like image 36
Benjamin Hodgson Avatar answered Sep 19 '22 00:09

Benjamin Hodgson