在Haskell中开发时查看未公开的库函数 [英] View non-exposed library functions while developing in Haskell

查看:139
本文介绍了在Haskell中开发时查看未公开的库函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个问题中犯了一个错误这可以通过查看

 :t myfunctionofinterest 


为我在库中使用的函数。



然而,当我在我的项目根目录下,并运行

  $ stack ghci 

我的Main.hs包含:

  import MyLib 



和我的模块一样:

  module MyLib { 
bunchOfFunctions - 但不是myfunctionofinterest
}其中

import SomeDB.ModuleThatExposes - myfunctionofinterest

myfunc :: IO()
myfunc = do
myfunctionofinterest abc - 我错误地使用myfunctionofinterest并且可能使用过的地方:t看它有3个参数

我不能:t myfunctionofi因为它没有公开,所以显式导入MyLib.myfunctionofinterest 帮助
,因为它是在导入中定义的。虽然我知道我可以公开它,然后检查它,:a 来编译,然后编辑lib以再次隐藏它,有没有什么可以更快更直接地实现?



这似乎是一种常见的模式。当你在开发时需要检查库中使用的类型时,你会做什么? 解决方案

引用 GHCi文档


:module 命令提供了一种做两件不能完成的事情的方法与普通的进口申报:


  • :模块支持


* 模块上的修饰符可打开模块的完整顶级范围,而不仅仅是导出。

额外的 * 使GHCi加载模块的字节码版本。这不会像性能一样,但是您可以访问未导出的绑定。



示例:

 λ> :m * MyLib 
λ> :t myfunctionofinterest

如果您得到

 模块'MyLib'不被解释;尝试':添加* MyLib的第一个

您可能必须执行:load 首先(关于:add 的建议并不总是有效):

 λ> :l * MyLib 
λ> :m * MyLib
λ> :t myfunctionofinterest


I made a mistake in another question that could have been solved by viewing

:t myfunctionofinterest

for a function I was using in a library.

However, when I am in my project root, and run

$ stack ghci

And my Main.hs has:

import MyLib

And my module does:

module MyLib {
  bunchOfFunctions -- but not myfunctionofinterest
} where

import SomeDB.ModuleThatExposes -- myfunctionofinterest

myfunc :: IO ()
myfunc = do
  myfunctionofinterest a b c -- place where I misuse myfunctionofinterest and could have used :t on it to see it had 3 args

I can't :t myfunctionofinterest in the main since it's not exposed, nor does Import MyLib.myfunctionofinterest explicitly help, since it was something defined in an import. While I know I could expose it then check it, :a to compile, and then edit the lib to hide it again, is there anything that allows that more quickly and directly?

This seems like it must be a common pattern. What do you do when you need to check the type of something used in a library as you develop?

解决方案

Quoting the GHCi docs:

The :module command provides a way to do two things that cannot be done with ordinary import declarations:

  • :module supports the * modifier on modules, which opens the full top-level scope of a module, rather than just its exports.

The additional * makes GHCi load the bytecode version of the module. This will not be as performant, but you'll get access to unexported bindings.

Example:

λ> :m *MyLib
λ> :t myfunctionofinterest

If you get

module 'MyLib' is not interpreted; try ':add *MyLib' first

you may have to do :load first (the advice about :add doesn't always do the trick):

λ> :l *MyLib
λ> :m *MyLib
λ> :t myfunctionofinterest

这篇关于在Haskell中开发时查看未公开的库函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆