Nyashのコード共有エコシステムを実現するためのexport
/import
構文仕様。「Everything is Box」哲学に基づき、Boxを中心とした明快な構文を提供する。
# 単一Boxのエクスポート
export box MathUtils {
init { precision }
factorial(n) {
if n <= 1 { return 1 }
return n * me.factorial(n - 1)
}
fibonacci(n) {
if n <= 1 { return n }
return me.fibonacci(n - 1) + me.fibonacci(n - 2)
}
}
# Static Boxのエクスポート
export static box Constants {
init { }
PI = 3.14159265359
E = 2.71828182846
GOLDEN_RATIO = 1.61803398875
}
# 複数Boxの名前付きエクスポート
export {
MathUtils,
Constants,
StringHelpers as StrUtils # エイリアス付き
}
# デフォルトエクスポート
export default box Calculator {
init { display }
// ...
}
# 名前付きインポート
import { MathUtils } from "math_utils.ny"
import { MathUtils, Constants } from "math_lib.ny"
# エイリアス付きインポート
import { MathUtils as Math } from "math_utils.ny"
# デフォルトインポート
import Calculator from "calculator.ny"
# 全体インポート(名前空間)
import * as MathLib from "math_lib.ny"
# 複合インポート
import Calculator, { MathUtils, Constants } from "advanced_calc.ny"
# 相対パス
import { Utils } from "./utils.ny"
import { Common } from "../common/helpers.ny"
# パッケージ名(nyash_modules/から)
import { Logger } from "awesome-logger"
# 絶対パス(非推奨、移植性のため)
import { Config } from "/home/user/project/config.ny"
./
または../
で始まる)nyash_modules/
ディレクトリmy-math-package/
├── nyash.toml # パッケージメタデータ
├── src/
│ ├── index.ny # メインエントリーポイント
│ ├── utils.ny
│ └── advanced.ny
├── tests/
│ └── test_math.ny
└── README.md
[package]
name = "awesome-math"
version = "1.0.0"
description = "素晴らしい数学ユーティリティ"
author = "Nyash Developer"
license = "MIT"
[dependencies]
# 他のNyashパッケージへの依存
basic-utils = "^2.0.0"
[export]
# パッケージのメインエクスポート
main = "src/index.ny"
# 内部モジュールをインポート
import { InternalUtils } from "./utils.ny"
import { AdvancedMath } from "./advanced.ny"
# 外部にエクスポート
export {
InternalUtils as Utils,
AdvancedMath
}
# デフォルトエクスポート
export default box MathPackage {
init {
me.utils = new Utils()
me.advanced = new AdvancedMath()
}
}
# プラットフォーム別エクスポート
if PLATFORM == "web" {
export { WebLogger as Logger } from "./web_logger.ny"
} else {
export { ConsoleLogger as Logger } from "./console_logger.ny"
}
# 他のモジュールから再エクスポート
export { MathUtils } from "./math_utils.ny"
export * from "./string_helpers.ny"
# 実行時に動的にインポート
local dynamicModule = await import("./heavy_module.ny")
local HeavyBox = dynamicModule.HeavyBox
export box SecureBox {
init {
_privateData # アンダースコアプレフィックスは慣習的にプライベート
publicData
}
# プライベートメソッド(エクスポートされない)
_internalProcess() {
// 内部処理
}
# パブリックメソッド
process() {
return me._internalProcess()
}
}
export box
構文import { Box } from "file"
構文export default
import * as namespace
as
)# ❌ エラー: 循環参照
# a.ny: import { B } from "./b.ny"
# b.ny: import { A } from "./a.ny"
# ✅ OK
import { Utils } from "./utils.ny"
# ❌ エラー: 関数内でのインポート
box MyBox {
method() {
import { Helper } from "./helper.ny" # エラー!
}
}
# ❌ エラー: 定義前のエクスポート
export { UndefinedBox } # エラー!
box UndefinedBox { }
機能 | Nyash | JavaScript | Python | Rust |
---|---|---|---|---|
名前付きexport | ✅ | ✅ | ✅ | ✅ |
デフォルトexport | ✅ | ✅ | ❌ | ❌ |
名前空間import | ✅ | ✅ | ✅ | ✅ |
動的import | 🔄 | ✅ | ✅ | ❌ |
再export | ✅ | ✅ | ✅ | ✅ |
# math_lib.ny
export box Vector2D {
init { x, y }
add(other) {
return new Vector2D(me.x + other.x, me.y + other.y)
}
magnitude() {
return Math.sqrt(me.x * me.x + me.y * me.y)
}
}
export static box MathConstants {
init { }
TAU = 6.28318530718
}
# game.ny
import { Vector2D, MathConstants } from "./math_lib.ny"
box Player {
init {
me.position = new Vector2D(0, 0)
me.velocity = new Vector2D(1, 1)
}
update() {
me.position = me.position.add(me.velocity)
local angle = MathConstants.TAU / 4 # 90度
}
}
Everything is Box - そしてBoxは共有される