Elixir exto.Repo.delete/2:查看类型规范时,如何知道是否存在默认值?

Elixir exto.Repo.delete/2:查看类型规范时,如何知道是否存在默认值?,elixir,phoenix-framework,Elixir,Phoenix Framework,以下是型号规格: iex(1)> h Ecto.Repo.delete No documentation for function Ecto.Repo.delete was found, but there is a callback with the same name. You can view callback documentations with the b/1 helper. iex(2)> b Ecto.Repo.delete @callback delete(

以下是型号规格:

iex(1)> h Ecto.Repo.delete
No documentation for function Ecto.Repo.delete was found, but there is a callback with the same name.
You can view callback documentations with the b/1 helper.

iex(2)> b Ecto.Repo.delete
@callback delete(
            struct_or_changeset :: 
              Ecto.Schema.t() | Ecto.Changeset.t(),
            opts :: Keyword.t()
          ) ::
            {:ok, Ecto.Schema.t()}
            | {:error, Ecto.Changeset.t()}

Deletes a struct using its primary key.

If the struct has no primary key,
Ecto.NoPrimaryKeyFieldError will be raised. If the struct
has been removed from db prior to call,
Ecto.StaleEntryError will be raised.

It returns {:ok, struct} if the struct has been
successfully deleted or {:error, changeset} if there was a
validation or a known constraint error.

## Options

  • :prefix - The prefix to run the query on (such as
    the schema path in Postgres or the database in MySQL).
    This overrides the prefix set in the query and any
    @schema_prefix set in the schema.
  • :stale_error_field - The field where stale errors
    will be added in the returning changeset. This option
    can be used to avoid raising Ecto.StaleEntryError.
  • :stale_error_message - The message to add to the
    configured :stale_error_field when stale errors happen,
    defaults to "is stale".

See the "Shared options" section at the module
documentation.

## Example

    post = MyRepo.get!(Post, 42)
    case MyRepo.delete post do
      {:ok, struct}       -> # Deleted with success
      {:error, changeset} -> # Something went wrong
    end 
在最后的示例中,使用一个参数调用delete()。而且,我可以通过一个参数成功调用
delete()

  def delete_item(%Auction.Item{}=item) do
    @repo.delete(item)  #<=== HERE
  end
def delete_item(%Auction.item{}=item)do

@repo.delete(item)#浏览文档和a,TypeSpec中似乎不支持默认参数。从2014年开始,这就解释了这种功能的一些权衡


你怎么知道第二个参数是可选的?除了长生不老药代码的源代码,希望它是有文档记录的。在这个版本的
exto.Repo.delete
中,参数在typespec中被命名为
opts
,并在标题“Options”下描述,我强烈建议它们是可选的。

浏览文档和a,typespec中似乎不支持默认参数。从2014年开始,这就解释了这种功能的一些权衡


你怎么知道第二个参数是可选的?除了长生不老药代码的源代码,希望它是有文档记录的。对于这个版本的
exto.Repo.delete
,参数在typespec中被命名为
opts
,并在标题“Options”下描述,我强烈建议它们是可选的。

这取决于回调的实现方式

在回调规范中不能有默认参数,但可以使用实现回调的默认参数定义函数

defmodule Converter do
  @doc """
  Callback doc.
  """
  @callback convert(number(), any(), any()) :: number()
end

defmodule MyConverter do
  @behaviour Converter

  def convert(number, from_unit \\ :km, to_unit \\ :miles) do
    # ...
  end
end

但是,这还不足以让
IEx.Helpers.h
宏实际显示默认参数,因为它适用于显式定义的文档

在上面的示例中,只定义了回调规范的
@doc
,因此将显示它

iex> h MyConverter.convert
@callback convert(number(), any(), any()) :: number()

Callback doc.
实际上,您需要为实现回调的函数定义
@doc
,以实现所需的功能

defmodule MyConverter do
  @behaviour Converter

  @doc """
  Implementation docs.
  """
  def convert(number, from_unit \\ :km, to_unit \\ :miles) do
    # ...
  end
end

iex> h MyConverter.convert

            def convert(number, from_unit \\ :km, to_unit \\ :miles)

Implementation docs.

现在,我们来谈谈您问题中概述的情况:当使用
exto.Repo
时,它在
MyApp.Repo
模块中定义了一个函数,该函数实现了
delete
回调,但没有模块文档。这就是为什么您无法通过IEx helpers查看此函数的默认参数。

这取决于回调的实现方式

在回调规范中不能有默认参数,但可以使用实现回调的默认参数定义函数

defmodule Converter do
  @doc """
  Callback doc.
  """
  @callback convert(number(), any(), any()) :: number()
end

defmodule MyConverter do
  @behaviour Converter

  def convert(number, from_unit \\ :km, to_unit \\ :miles) do
    # ...
  end
end

但是,这还不足以让
IEx.Helpers.h
宏实际显示默认参数,因为它适用于显式定义的文档

在上面的示例中,只定义了回调规范的
@doc
,因此将显示它

iex> h MyConverter.convert
@callback convert(number(), any(), any()) :: number()

Callback doc.
实际上,您需要为实现回调的函数定义
@doc
,以实现所需的功能

defmodule MyConverter do
  @behaviour Converter

  @doc """
  Implementation docs.
  """
  def convert(number, from_unit \\ :km, to_unit \\ :miles) do
    # ...
  end
end

iex> h MyConverter.convert

            def convert(number, from_unit \\ :km, to_unit \\ :miles)

Implementation docs.
现在,我们来谈谈您问题中概述的情况:当使用
exto.Repo
时,它在
MyApp.Repo
模块中定义了一个函数,该函数实现了
delete
回调,但没有模块文档。这就是为什么您无法通过IEx帮助程序查看此函数的默认参数