The benefit of importing specific parts of a Haskell module

Except for potential name conflicts that can be used in other ways, is there any use for importing only parts from the module you need:

import SomeModule (x, y, z) 

... poems simply import all of this, which simplifies and simplifies maintenance:

 import SomeModule 

Can make a binary smaller, for example?

+6
source share
2 answers

No, this is only to prevent name conflicts. Another collision avoidance mechanism - namely, import qualified - leads to more verbose (less readable) code.

This would not make the binary size smaller - consider that the functions in this module all refer to each other, as a rule, therefore they must be compiled together.

+6
source

Name mappings and binary size optimizations are just two of the benefits you can get. Indeed, it is always good practice to determine what you want to get from the outside world of your code. That way, whenever people look at your code, they will know what exactly your code is requesting.

It also gives you very good chances of creating mocking test solutions, as you can work through the list of imports and write mockery for them.

Unfortunately, in Haskell, instances of type classes are not so simple. They are imported implicitly and therefore can cause conflicts, and can also complicate the work, since it is not possible to specify only specific instances of the class. Hope this can be fixed in future versions of Haskell.

UPDATE

The features listed above (code maintenance and testing) are not limited to Haskell. Actually, this is also a common practice in Java, as I know. In Java, you can simply import one class or even one static variable / method. Unfortunately, you still cannot selectively import member functions.

+11
source

Source: https://habr.com/ru/post/956600/


All Articles