Benefit of importing specific parts of a Haskell module

前端 未结 2 1614
眼角桃花
眼角桃花 2021-01-12 16:03

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:



        
相关标签:
2条回答
  • 2021-01-12 16:49

    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.

    0 讨论(0)
  • 2021-01-12 16:56

    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.

    0 讨论(0)
提交回复
热议问题