跳到主要内容
知仓学习社ZHICANG

developing-julia-package

Use when you write a Julia package

不碰外部(只输出文字)无严重或高危命中hashgraph-online/awesome-codex-plugins

它会碰到什么

扫了多少1 个文本文件,4 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

Developing a Julia package

Notes on developing Julia packages.

Assume the package is named MyPkg. Substitute your actual package name wherever MyPkg appears.

Use src/MyPkg.jl as the package entry point. Keep the module declaration, exports, and include list there; put substantial implementation in focused files under src/.

module MyPkg

export fit_model, FitResult, GaussianModel

include("models.jl")
include("fit.jl")
include("preprocess.jl")

end

Split files to improve readability, not to recreate Python-style class or submodule hierarchies. Prefer one public module unless there is a real user-facing namespace boundary.

Guidelines below.

Avoid excessive exports

  • Do not export helpers that are only used internally just so tests can reach them. Prefer importing explicitly in tests:
# test/runtests.jl

using Test
using MyPkg: <internal-only helper>

Test public behavior through the public API first. Import internals only when the helper has meaningful behavior that is hard to exercise through the public path:

using Test
using MyPkg
using MyPkg: initial_guess

@testset "fit_model" begin
    result = fit_model(x, y; model = GaussianModel())
    @test result isa FitResult
end

@testset "initial_guess" begin
    @test initial_guess(x, y, GaussianModel()) isa NamedTuple
end

Julia-idiomatic style

Multiple dispatch

Prefer splitting behavior across methods instead of a large if/elseif chain on isa, unlike typical Python style.

# Do not write if else end
function f(x)
    if x isa Integer
        return 2x
    else
        return x
    end
end

Instead, use multiple dispatch:

f(x) = x # generic implementation
f(x::Integer) = 2x # specialized implementation for x::Integer

For package APIs, make the dispatch object explicit and keep symbol options as a thin compatibility layer if needed:

abstract type AbstractModel end

struct GaussianModel <: AbstractModel
    baseline::Bool
end

GaussianModel(; baseline = true) = GaussianModel(baseline)

fit_model(x, y; model::AbstractModel = GaussianModel()) =
    fit_model(model, x, y)

function fit_model(model::GaussianModel, x, y)
    guess = initial_guess(x, y, model)
    # gaussian-specific implementation
end

Use a marker or configuration struct for the model choice, and use a separate result struct for fitted values. Do not mutate a model type into a mixed "algorithm plus fitted state" object unless that is clearly the public contract.

struct FitResult{P,T}
    params::P
    residuals::Vector{T}
    converged::Bool
end

This keeps GaussianModel() as the method-selection/configuration value and FitResult as the returned fitted state.

Type annotations

  • On public APIs, narrow signatures when it prevents misuse or clarifies the contract.
  • Inside the package, avoid over-constraining types everywhere; leave room for the compiler and for generic code.

Type stability

  • On hot paths, avoid return types that vary unpredictably across inputs (type instability hurts specialization).
  • Confirm bottlenecks with profiling before micro-optimizing.

Code formatting (JuliaFormatter)

  • Format package sources with JuliaFormatter.jl so layout and whitespace stay consistent across contributors and CI.
  • Optionally commit a .JuliaFormatter.toml at the repository root (or rely on defaults) so everyone applies the same rules.

From the package root:

using JuliaFormatter
format(".")  # formats src/, test/, etc. under the current directory

Run formatting before merging substantive edits; wire the same command into CI or pre-commit hooks if the team wants enforcement.

Performance and allocations

  • Measure with @benchmark / @btime from BenchmarkTools.jl rather than guessing.
  • Watch unnecessary array copies from slicing and broadcasting; when an in-place API is needed, expose it explicitly (separate function name or keyword argument) so callers opt in.

Errors and documentation

  • Raise ArgumentError, DomainError, or other appropriate exceptions; messages should tell the caller what to fix.
  • Give docstrings to exported/public functions—ordinary docstrings above definitions integrate cleanly with Documenter.jl; use @doc when you attach documentation programmatically.

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 1,027
本站分层T1
该仓技能数1910
原文件路径plugins/AtelierArith/atelier-arith-julia-development-skills/skills/developing-julia-package/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 1910 个技能