如何导入具有不同数量参数的函数
How to import a function with different number of params
假设我有一个模块:
defmodule Request do
def send(url, body \ "", headers \ [], opts \ []) do
.. some code
end
... other functions ...
end
我只需要导入 send
函数,所以我会这样写:
import Request, only: [send: 4]
但这里的问题是如果我使用 send
函数只传递 url 参数并接受默认值,编译器会给我一个错误说 send
未定义对于其余参数。
所以我想知道是否有一种方法可以在不导入send
、send
和send
的情况下导入整个send
函数
像 Request.send
那样使用通过模块本身访问它的函数可能更有意义,但我仍然对答案感兴趣
您无法调用 send/1
,因为您尚未导入它(正如您所识别的那样。)
您的选择是:
import Request
import Request: :functions
import Request, only: [send: 1, send: 2, send: 3, send: 4]
import Request, except: [notsend: 1, also_notsend: 2]
您可以在 https://hexdocs.pm/elixir/Kernel.SpecialForms.html#import/2
阅读文档
假设我有一个模块:
defmodule Request do
def send(url, body \ "", headers \ [], opts \ []) do
.. some code
end
... other functions ...
end
我只需要导入 send
函数,所以我会这样写:
import Request, only: [send: 4]
但这里的问题是如果我使用 send
函数只传递 url 参数并接受默认值,编译器会给我一个错误说 send
未定义对于其余参数。
所以我想知道是否有一种方法可以在不导入send
、send
和send
send
函数
像 Request.send
那样使用通过模块本身访问它的函数可能更有意义,但我仍然对答案感兴趣
您无法调用 send/1
,因为您尚未导入它(正如您所识别的那样。)
您的选择是:
import Request
import Request: :functions
import Request, only: [send: 1, send: 2, send: 3, send: 4]
import Request, except: [notsend: 1, also_notsend: 2]
您可以在 https://hexdocs.pm/elixir/Kernel.SpecialForms.html#import/2
阅读文档