elixir - Ecto.Repo.delete/2:查看类型规范时,如何知道是否存在默认值?
问题描述
这是类型规范:
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
所以,这告诉我第二个参数必须有一个默认值。有没有办法以某种方式知道这一点?
解决方案
浏览文档和类似问题的答案,类型规范似乎不支持默认参数。这个 2014 年的 Github 问题解释了对此类功能的一些权衡。
怎么知道第二个参数是可选的?除了对 Elixir 代码进行源代码挖掘之外,希望它被记录在案。在这个版本的情况下Ecto.Repo.delete
,参数opts
在 typespec 中命名,并在标题“选项”下描述,这对我来说强烈暗示它们是可选的。
推荐阅读
- sqlite - 如何在 sqlite 中工作时遇到冲突忽略
- methods - 当拆分方法无法正常工作时比我预期的?
- python - 如何找到链表中的最大元素?
- php - 查询深度嵌套的属性会运行无效的 PathExpression。必须是 StateFieldPathExpression 错误
- sql - Postgres如何分解这个导入不同模式的问题,需要将一个类别数组“扇出”到一个类别表中
- php - PHP使用索引数组作为数组索引
- git - Git:只检测丢失的部分,而不是那些刚刚移动的部分
- javascript - 使用 javascript 在所有网页上插入页眉和页脚
- python-3.x - 复制粘贴不适用于 python selenium 中的无头浏览器
- php - WordPress:编辑器中锚标签中添加的属性会在一段时间后消失,而不会留下来