Guides writing Elixir documentation with @moduledoc, @doc, @typedoc, doctests, cross-references, and metadata. Use when adding or improving documentation in .ex files.
| 主题 | 参考 |
|---|---|
| 文档测试:语法、注意事项、使用时机 | references/doctests.md |
| 交叉引用和链接语法 |
ExDoc 和 mix docs 等工具会提取 @moduledoc 和 @doc 的第一段作为摘要。请保持首行简洁且独立完整。
elixir
封装 Stripe API 客户端,确保在向调用方返回确认之前,每笔费用都已记录在本地账本中。
它使用 Stripe 并同时更新账本。
同样的规则适用于 @doc:
elixir
成功时返回 {:ok, charge},支付网关拒绝请求时返回 {:error, reason}。
一个结构良好的 @moduledoc 遵循以下模式:
elixir
defmodule MyApp.Inventory do
@moduledoc
跟踪仓库库存水平并触发补货订单。
本模块维护一个基于 ETS 的当前数量缓存,并暴露用于原子库存调整的函数。
设计在监督器下启动,并在初始化时从数据库恢复状态。
## 示例
iex> {:ok, pid} = MyApp.Inventory.start_link(warehouse: :east)
iex> MyApp.Inventory.current_stock(pid, SKU-1042)
{:ok, 350}
## 配置
在 config/runtime.exs 中期望以下配置:
config :my_app, MyApp.Inventory,
repo: MyApp.Repo,
lowstockthreshold: 50
end
关键点:
在定义行为时,记录预期的回调:
elixir
defmodule MyApp.PaymentGateway do
@moduledoc
支付网关集成的行为。
实现必须处理收费、退款和状态检查。
请参阅 MyApp.PaymentGateway.Stripe 获取参考实现。
## 回调
* charge/2 - 发起指定金额的收费
* refund/2 - 退款先前完成的收费
* status/1 - 检查交易状态
@callback charge(amount :: pos_integer(), currency :: atom()) ::
{:ok, transaction_id :: String.t()} | {:error, term()}
@callback refund(transactionid :: String.t(), amount :: posinteger()) ::
:ok | {:error, term()}
@callback status(transaction_id :: String.t()) ::
{:pending | :completed | :failed, map()}
end
elixir
@doc
预留指定数量的物品,减少可用库存。
当库存可用时返回 {:ok, reservation_id},当请求数量超过现有库存时返回
{:error, :insufficient_stock}。
iex> MyApp.Inventory.reserve(SKU-1042, 5)
{:ok, res_abc123}
iex> MyApp.Inventory.reserve(SKU-9999, 1)
{:error, :not_found}
* :warehouse - 目标仓库原子。默认为 :primary。
* :timeout - 超时时间(毫秒)。默认为 5_000。
@spec reserve(String.t(), pos_integer(), keyword()) ::
{:ok, String.t()} | {:error, :insufficientstock | :notfound}
def reserve(sku, quantity, opts \\ []) do
# ...
end
指南:
记录使用 @type 或 @opaque 定义的自定义类型:
elixir
@typedoc
表示最小货币单位(例如,分)金额的正整数。
@type amount :: pos_integer()
@typedoc
由 status/1 返回的预留状态。
* :held - 库存已预留但尚未发货
* :released - 预留已取消,库存已恢复
* :fulfilled - 物品已发货
@type reservation_status :: :held | :released | :fulfilled
@typedoc
由 connect/1 返回的不透明句柄。请勿对此值进行模式匹配。
@opaque connection :: %MODULE{socket: port(), buffer: binary()}
对于 @opaque 类型,@typedoc 尤其重要,因为调用方无法检查其结构。
elixir
@doc since: 1.3.0
@doc
在两个仓库之间转移库存。
def transfer(from, to, sku, quantity), do: # ...
@doc deprecated: 请改用 transfer/4
@doc
在位置之间移动物品。已弃用,请改用支持跨区域转移的 transfer/4。
def move_stock(from, to, sku, quantity), do: # ...
可以在一个属性中组合元数据和文档字符串:
elixir
@doc since: 2.0.0, deprecated: 请改用 bulk_reserve/2
@doc
在单次调用中预留多个物品。
def batch_reserve(items), do: # ...
@moduledoc since: 对模块的工作方式相同:
elixir
@moduledoc since: 1.2.0
@moduledoc
处理 Stripe 事件的 Webhook 签名验证。
当模块或函数不属于公共 API 时,抑制文档:
elixir
不要在真正的公共函数上使用 @doc false。 如果函数被导出且调用方依赖它,请为其编写文档。如果不应该被外部调用,请使用 defp 将其设为私有。
| 文档(@moduledoc、@doc) | 代码注释(#) | |
|---|---|---|
| 受众 | API 的用户 | 阅读源代码的开发者 |
| 目的 |
elixir
@doc
验证给定的优惠券代码是否处于激活状态且还有剩余使用次数。
@spec validate_coupon(String.t()) :: {:ok, Coupon.t()} | {:error, :expired | :exhausted}
def validate_coupon(code) do
# 我们在此处查询只读副本,以避免在高
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 elixir-writing-docs-1776114548 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 elixir-writing-docs-1776114548 技能
skillhub install elixir-writing-docs-1776114548
文件大小: 12.3 KB | 发布时间: 2026-4-14 13:17