docs: reorganize into 説明書/予定/archive; update docs/README.md and CLAUDE.md; move root .nyash to local_tests; consolidate native-plan notes into README + archive
This commit is contained in:
@ -1,182 +0,0 @@
|
||||
# ビルトインBox型 API リファレンス
|
||||
|
||||
Nyashで利用できる全ビルトインBox型のAPI仕様書です。
|
||||
|
||||
## 📡 P2PBox - 通信ノードBox
|
||||
|
||||
P2P通信を行うノードを表すBox。通信世界(IntentBox)に参加してメッセージを送受信できます。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
// 通信ノードを作成
|
||||
local node = new P2PBox(node_id, world)
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `node_id` (String): ノードの一意識別子
|
||||
- `world` (IntentBox): 参加する通信世界
|
||||
|
||||
### メソッド
|
||||
|
||||
#### send(intent, data, target)
|
||||
特定のノードにメッセージを送信します。
|
||||
```nyash
|
||||
local result = node.send("greeting", message_data, "target_node_id")
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `intent` (String): メッセージの種類
|
||||
- `data` (Box): 送信するデータ
|
||||
- `target` (String): 送信先ノードID
|
||||
|
||||
**戻り値:** StringBox("sent")
|
||||
|
||||
#### on(intent, callback)
|
||||
指定したintentのメッセージを受信した際のリスナーを登録します。
|
||||
```nyash
|
||||
node.on("chat", callback_function)
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `intent` (String): 監視するメッセージ種類
|
||||
- `callback` (MethodBox): 受信時に呼ばれる関数
|
||||
|
||||
**戻り値:** StringBox("listener added")
|
||||
|
||||
#### off(intent)
|
||||
指定したintentのリスナーを解除します。
|
||||
```nyash
|
||||
node.off("chat")
|
||||
```
|
||||
|
||||
**パラメータ:**
|
||||
- `intent` (String): 解除するメッセージ種類
|
||||
|
||||
**戻り値:** StringBox("listener removed" / "no listener found")
|
||||
|
||||
#### get_node_id()
|
||||
このノードのIDを取得します。
|
||||
```nyash
|
||||
local id = node.get_node_id()
|
||||
```
|
||||
|
||||
**戻り値:** StringBox(ノードID)
|
||||
|
||||
### 使用例
|
||||
```nyash
|
||||
// 通信世界を作成
|
||||
local world = new IntentBox()
|
||||
|
||||
// 2つのノードを作成
|
||||
local alice = new P2PBox("alice", world)
|
||||
local bob = new P2PBox("bob", world)
|
||||
|
||||
// Bobがgreetingを受信するリスナー設定
|
||||
bob.on("greeting", greeting_handler)
|
||||
|
||||
// AliceからBobにメッセージ送信
|
||||
local message = new MapBox()
|
||||
message.set("text", "Hello Bob!")
|
||||
alice.send("greeting", message, "bob")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📨 IntentBox - 通信世界Box
|
||||
|
||||
P2PBoxが通信を行うための世界(ネットワーク)を表すBox。複数のノードが同一のIntentBoxを共有して通信します。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
// 通信世界を作成
|
||||
local world = new IntentBox()
|
||||
```
|
||||
|
||||
**パラメータ:** なし
|
||||
|
||||
### 特徴
|
||||
- ローカル通信: 同一プロセス内のP2PBox間でメッセージをやり取り
|
||||
- スレッドセーフ: Arc<Mutex>により並行アクセス対応
|
||||
- 将来拡張: WebSocket版や分散版への拡張予定
|
||||
|
||||
### 使用例
|
||||
```nyash
|
||||
// 1つの通信世界に複数ノードが参加
|
||||
local world = new IntentBox()
|
||||
local node1 = new P2PBox("server", world)
|
||||
local node2 = new P2PBox("client", world)
|
||||
|
||||
// 同一世界内での通信が可能
|
||||
node1.send("data", payload, "client")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 📝 StringBox - 文字列Box
|
||||
|
||||
文字列データを格納・操作するBox。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
local text = new StringBox("Hello")
|
||||
```
|
||||
|
||||
### 基本メソッド
|
||||
- `toString()`: 文字列表現を取得
|
||||
- `length()`: 文字列長を取得
|
||||
- `concat(other)`: 文字列結合
|
||||
- `substring(start, end)`: 部分文字列取得
|
||||
|
||||
---
|
||||
|
||||
## 🔢 IntegerBox - 整数Box
|
||||
|
||||
整数データを格納・操作するBox。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
local num = new IntegerBox(42)
|
||||
```
|
||||
|
||||
### 基本メソッド
|
||||
- `toString()`: 文字列表現を取得
|
||||
- `add(other)`: 加算
|
||||
- `subtract(other)`: 減算
|
||||
- `multiply(other)`: 乗算
|
||||
- `divide(other)`: 除算
|
||||
|
||||
---
|
||||
|
||||
## 📺 ConsoleBox - コンソール出力Box
|
||||
|
||||
コンソールへの出力を行うBox。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
local console = new ConsoleBox()
|
||||
```
|
||||
|
||||
### メソッド
|
||||
- `log(message)`: メッセージをログ出力
|
||||
- `error(message)`: エラーメッセージを出力
|
||||
|
||||
---
|
||||
|
||||
## 🗂️ MapBox - 連想配列Box
|
||||
|
||||
キー・バリューペアでデータを格納するBox。
|
||||
|
||||
### コンストラクタ
|
||||
```nyash
|
||||
local map = new MapBox()
|
||||
```
|
||||
|
||||
### メソッド
|
||||
- `set(key, value)`: キー・バリューを設定
|
||||
- `get(key)`: 値を取得
|
||||
- `has(key)`: キーが存在するかチェック
|
||||
- `remove(key)`: キー・バリューを削除
|
||||
|
||||
---
|
||||
|
||||
最終更新: 2025年8月11日
|
||||
@ -1,337 +0,0 @@
|
||||
# 🌟 明示的デリゲーション革命:なぜNyashは世界初の完全明示デリゲーション言語になったのか
|
||||
|
||||
作成日: 2025年8月11日
|
||||
著者: Nyashプロジェクトチーム
|
||||
ステータス: 設計思想決定版
|
||||
|
||||
## 📜 はじめに:革命の始まり
|
||||
|
||||
2025年8月11日、Nyashプログラミング言語の開発において、言語設計史上最大級の発見がありました。それは、**暗黙のオーバーライド問題**の発見と、それを解決する**完全明示デリゲーション構文**の誕生です。
|
||||
|
||||
この文書は、なぜこの革命が必要だったのか、どのような思想の元に設計されたのかを詳しく解説します。
|
||||
|
||||
## 🚨 問題の発見:暗黙の悪魔
|
||||
|
||||
### HashMap::insert による意図しない上書き
|
||||
|
||||
Nyashの実装を詳しく調査した結果、恐ろしい問題が発見されました:
|
||||
|
||||
```rust
|
||||
// instance.rs - add_method関数
|
||||
pub fn add_method(&mut self, method_name: String, method_ast: ASTNode) {
|
||||
let mut new_methods = (*self.methods).clone();
|
||||
new_methods.insert(method_name, method_ast); // ← 暗黙の上書き!
|
||||
self.methods = Arc::new(new_methods);
|
||||
}
|
||||
```
|
||||
|
||||
この実装により、以下のような**暗黙のオーバーライド**が発生していました:
|
||||
|
||||
```nyash
|
||||
box Node {
|
||||
send(msg) { // 最初の定義
|
||||
print("Version 1")
|
||||
}
|
||||
|
||||
send(msg) { // 暗黙に上書きされる
|
||||
print("Version 2") // ← こちらだけが残る
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Nyash哲学との根本的矛盾
|
||||
|
||||
この問題は、Nyashの3つの核心哲学と完全に矛盾していました:
|
||||
|
||||
1. **明示性重視**: 「何が起きているかを隠さない」
|
||||
2. **Everything is Box**: 「統一された世界観」
|
||||
3. **初学者フレンドリー**: 「複雑な概念を分かりやすく表現」
|
||||
|
||||
暗黙のオーバーライドは、これらすべてを破壊する**言語設計上の致命的欠陥**だったのです。
|
||||
|
||||
## 💡 解決への道:3AI大会議
|
||||
|
||||
### AI専門家による徹底分析
|
||||
|
||||
この問題の解決策を求めて、言語設計の専門家であるGeminiとChatGPTに相談を行いました。結果は予想を上回る**圧倒的な支持**でした。
|
||||
|
||||
#### Gemini先生の評価
|
||||
> **「全面的に賛成します」**
|
||||
> **「極めて重要な一歩」**
|
||||
> **「Nyashのアイデンティティを確立する」**
|
||||
|
||||
#### ChatGPT先生の評価
|
||||
> **「強く整合する」**
|
||||
> **「安全性と読みやすさを大幅に向上」**
|
||||
> **「実装工数3-5日程度」**
|
||||
|
||||
### 専門的視点からの裏付け
|
||||
|
||||
両専門家から以下の重要な指摘がありました:
|
||||
|
||||
1. **哲学的整合性**: Nyashの明示性哲学と完全に合致
|
||||
2. **技術的優位性**: 他言語の問題(Python MRO、Java super等)を根本解決
|
||||
3. **学習効果**: 初学者にとってより理解しやすい設計
|
||||
4. **実装可能性**: 技術的に十分実現可能
|
||||
|
||||
## 🌟 革命的解決策:Override + From 統一構文
|
||||
|
||||
### 4つの統一原則
|
||||
|
||||
この問題を解決するため、以下の4つの統一原則を確立しました:
|
||||
|
||||
#### 1. 宣言の統一
|
||||
```nyash
|
||||
box Child from Parent // デリゲーション関係の明示
|
||||
```
|
||||
|
||||
#### 2. 置換の統一
|
||||
```nyash
|
||||
override methodName() // オーバーライドの明示宣言
|
||||
```
|
||||
|
||||
#### 3. 呼び出しの統一
|
||||
```nyash
|
||||
from Parent.methodName() // 親実装の明示呼び出し
|
||||
```
|
||||
|
||||
#### 4. 構築の統一
|
||||
```nyash
|
||||
from Parent.init() // コンストラクタも同じ構文
|
||||
```
|
||||
|
||||
### 完全な例
|
||||
|
||||
```nyash
|
||||
box MeshNode : P2PBox {
|
||||
init routing = RoutingTable()
|
||||
|
||||
constructor(nodeId, world) {
|
||||
from P2PBox.constructor(nodeId, world) // 統一構文
|
||||
me.routing = RoutingTable()
|
||||
}
|
||||
|
||||
override send(intent, data, target) { // 明示的置換
|
||||
me.routing.log(target)
|
||||
from P2PBox.send(intent, data, target) // 明示的呼び出し
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 🔥 革命的特徴
|
||||
|
||||
### 1. 完全な明示性
|
||||
|
||||
**従来の問題**:
|
||||
- 何がオーバーライドされているかわからない
|
||||
- 親のどのメソッドを呼んでいるかわからない
|
||||
- 実行順序が不明確
|
||||
|
||||
**Nyashの解決**:
|
||||
- `override` で置換を明示宣言
|
||||
- `from Parent.method()` で呼び出し先を完全明示
|
||||
- 上から下への直感的な実行順序
|
||||
|
||||
### 2. 曖昧性の完全排除
|
||||
|
||||
**多重デリゲーション時の曖昧性解消**:
|
||||
```nyash
|
||||
box SmartNode : P2PBox, Logger {
|
||||
override send(intent, data, target) {
|
||||
from Logger.debug("Sending: " + intent) // どのLoggerか明確
|
||||
from P2PBox.send(intent, data, target) // どのP2PBoxか明確
|
||||
}
|
||||
}
|
||||
|
||||
// 競合時は更に明示的に
|
||||
box ConflictNode from ParentA, ParentB {
|
||||
override ParentA.process(data) { // ParentAのprocessを置換
|
||||
from ParentA.process(data)
|
||||
}
|
||||
|
||||
override ParentB.process(data) { // ParentBのprocessを置換
|
||||
from ParentB.process(data)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 学習コストの最小化
|
||||
|
||||
**覚えるべきルール**:
|
||||
1. 親のメソッドを置換したい → `override`
|
||||
2. 親のメソッドを呼びたい → `from Parent.method()`
|
||||
3. 親のコンストラクタを呼びたい → `from Parent.init()`
|
||||
|
||||
たった3つのルールで、すべてのデリゲーション操作が表現できます。
|
||||
|
||||
## 🌍 他言語との比較:なぜNyashが優れているのか
|
||||
|
||||
### Python の問題
|
||||
```python
|
||||
# MRO(Method Resolution Order)地獄
|
||||
class C(A, B):
|
||||
def method(self):
|
||||
super().method() # どっちのmethod?
|
||||
```
|
||||
|
||||
**Nyash の解決**:
|
||||
```nyash
|
||||
box C : A, B {
|
||||
override method() {
|
||||
from A.method() // Aのmethodと明示
|
||||
from B.method() // Bのmethodと明示
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Java/C# の問題
|
||||
```java
|
||||
// どの親のmethodを呼んでいるかコードから不明
|
||||
@Override
|
||||
public void method() {
|
||||
super.method(); // 単一継承でも曖昧
|
||||
}
|
||||
```
|
||||
|
||||
**Nyash の解決**:
|
||||
```nyash
|
||||
override method() {
|
||||
from Parent.method() // どのParentか完全に明確
|
||||
}
|
||||
```
|
||||
|
||||
### TypeScript の問題
|
||||
```typescript
|
||||
// 暗黙のオーバーライドによる事故
|
||||
class Child extends Parent {
|
||||
method() { // うっかり同名メソッド → 意図しない上書き
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Nyash の解決**:
|
||||
```nyash
|
||||
// overrideなしで同名メソッド → コンパイルエラー
|
||||
// 意図しない上書きは100%防止
|
||||
```
|
||||
|
||||
## 🎯 設計思想の深層
|
||||
|
||||
### Everything is Box との統合
|
||||
|
||||
この革命は、Nyashの根本思想「Everything is Box」と完全に統合されています:
|
||||
|
||||
- **Box同士のデリゲーション**: 階層ではなく、協力関係
|
||||
- **Boxメソッドの明示的管理**: どのBoxのどのメソッドかが常に明確
|
||||
- **Box構築の明示的制御**: コンストラクタも普通のメソッド
|
||||
|
||||
### 明示性の哲学
|
||||
|
||||
Nyashが目指すのは、**「魔法のない言語」**です:
|
||||
|
||||
- 隠れた処理は一切なし
|
||||
- すべての動作がコードに現れる
|
||||
- 初学者でも上級者でも同じように理解できる
|
||||
|
||||
### 初学者への配慮
|
||||
|
||||
複雑な概念を、シンプルな文法で表現:
|
||||
|
||||
- `override` = 「置き換えます」
|
||||
- `from Parent.method()` = 「親の方法を使います」
|
||||
- コンパイルエラー = 「間違いを素早く教える」
|
||||
|
||||
## 🚀 実装戦略
|
||||
|
||||
### 段階的導入
|
||||
|
||||
ChatGPT先生の提案による実装ロードマップ:
|
||||
|
||||
**Phase 1(0.5-1日)**:
|
||||
- `override` キーワード追加
|
||||
- 基本パーサー拡張
|
||||
|
||||
**Phase 2(1-2日)**:
|
||||
- 暗黙オーバーライド検出
|
||||
- コンストラクタ重複禁止
|
||||
|
||||
**Phase 3(1日)**:
|
||||
- `from Parent.init()` 実装
|
||||
- エラーメッセージ改善
|
||||
|
||||
### 移行支援
|
||||
|
||||
既存コードの安全な移行:
|
||||
- 段階的警告システム
|
||||
- 自動修正支援ツール
|
||||
- 詳細な移行ガイド
|
||||
|
||||
## 🌟 期待される効果
|
||||
|
||||
### 1. 開発者体験の革命的向上
|
||||
|
||||
**Before(暗黙オーバーライド)**:
|
||||
- バグの発見が困難
|
||||
- 意図しない動作
|
||||
- デバッグに多大な時間
|
||||
|
||||
**After(明示的オーバーライド)**:
|
||||
- コンパイル時に間違いを検出
|
||||
- 意図が明確に表現される
|
||||
- デバッグ時間の劇的短縮
|
||||
|
||||
### 2. コードの可読性向上
|
||||
|
||||
**Before**:
|
||||
```nyash
|
||||
// これは何をオーバーライドしている?
|
||||
send(msg) {
|
||||
// 親を呼んでる?呼んでない?
|
||||
processMessage(msg)
|
||||
}
|
||||
```
|
||||
|
||||
**After**:
|
||||
```nyash
|
||||
// P2PBoxのsendを明示的にオーバーライド
|
||||
override send(msg) {
|
||||
processMessage(msg)
|
||||
from P2PBox.send(msg) // P2PBoxの実装も使用
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 保守性の向上
|
||||
|
||||
- 変更の影響範囲が明確
|
||||
- リファクタリングが安全
|
||||
- チーム開発での誤解を防止
|
||||
|
||||
## 🏆 結論:言語設計史に残る革命
|
||||
|
||||
この明示的デリゲーション革命により、Nyashは以下を達成しました:
|
||||
|
||||
### 世界初の完全明示デリゲーション言語
|
||||
|
||||
1. **完全な明示性**: すべての動作を明示
|
||||
2. **曖昧性の完全排除**: どんな複雑なケースも明確
|
||||
3. **統一構文**: デリゲーションとオーバーライドの完全統合
|
||||
4. **初学者フレンドリー**: 学習しやすく、間違いにくい
|
||||
|
||||
### プログラミング言語設計への貢献
|
||||
|
||||
- **暗黙の悪魔**からの完全な解放
|
||||
- **多重デリゲーション**の安全で明確な実現
|
||||
- **コード可読性**の新しい基準の確立
|
||||
|
||||
### 未来への影響
|
||||
|
||||
Nyashのこの革命は、今後のプログラミング言語設計に大きな影響を与えるでしょう。「暗黙より明示」という哲学が、ついに技術的に完全実現されたのです。
|
||||
|
||||
---
|
||||
|
||||
**2025年8月11日は、プログラミング言語史において「明示的デリゲーション革命の日」として記憶されることでしょう。** 🎊
|
||||
|
||||
この革命により、Nyashは単なるプログラミング言語を超えて、**新しいプログラミングパラダイムの先駆者**となりました。
|
||||
|
||||
Everything is Box. Everything is Explicit. Everything is Beautiful. 🌟
|
||||
@ -1,252 +0,0 @@
|
||||
# 🔥 Nyash finiシステム - 論理的解放フック
|
||||
|
||||
**最終更新: 2025年8月13日 - ChatGPT5協議による革命的設計完了**
|
||||
|
||||
## 🎯 概要
|
||||
|
||||
Nyashの`fini()`システムは、物理的メモリ破棄ではなく**論理的使用終了**を宣言する革新的なリソース管理システムです。Everything is Box哲学と完全に統合され、予測可能で安全なリソース管理を実現します。
|
||||
|
||||
## 🌟 核心コンセプト
|
||||
|
||||
### 📝 論理的解放フック
|
||||
```nyash
|
||||
box MyResource {
|
||||
init { name, file }
|
||||
|
||||
fini() {
|
||||
print("Resource " + me.name + " is being finalized")
|
||||
// ファイルクローズなどのクリーンアップ処理
|
||||
// 物理的メモリは共有参照が残っていても論理的には「終了」
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**重要**: `fini()`は「このオブジェクトをもう使わない」という宣言であり、物理的な即時破棄ではありません。
|
||||
|
||||
## 🔄 実行順序(最終仕様)
|
||||
|
||||
### 自動カスケード解放
|
||||
```nyash
|
||||
box Pipeline {
|
||||
init { r1, r2, r3, weak monitor }
|
||||
|
||||
fini() {
|
||||
// 1) ユーザー定義処理(柔軟な順序制御可能)
|
||||
me.r3.fini() // 依存関係でr3→r2の順
|
||||
me.r2.fini()
|
||||
|
||||
// 2) 自動カスケード: 残りのr1がinit宣言順で自動解放
|
||||
// 3) weakフィールドは対象外(lazy nil化)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 決定的な解放順序
|
||||
1. **finalized チェック** - 既に解放済みなら何もしない(idempotent)
|
||||
2. **再入防止** - `in_finalization`フラグで再帰呼び出し防止
|
||||
3. **ユーザー定義fini()実行** - カスタムクリーンアップ処理
|
||||
4. **自動カスケード** - `init`宣言順で未処理フィールドを解放
|
||||
5. **フィールドクリア** - 全フィールドを無効化
|
||||
6. **finalized設定** - 以後の使用を禁止
|
||||
|
||||
## ⚠️ 厳格な禁止事項
|
||||
|
||||
### weak フィールドへのfini呼び出し禁止
|
||||
```nyash
|
||||
box Parent {
|
||||
init { weak child }
|
||||
|
||||
fini() {
|
||||
// ❌ 絶対にダメ!ビルドエラーまたは実行時エラー
|
||||
// me.child.fini()
|
||||
|
||||
// ✅ 正しい方法
|
||||
me.child = null // 参照解除
|
||||
// または自動nil化に任せる
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**理由**: weak参照は所有権を持たない非所有参照のため、fini()を呼ぶ権利がありません。
|
||||
|
||||
### finalized後の使用禁止
|
||||
```nyash
|
||||
box Example { }
|
||||
|
||||
local x = new Example()
|
||||
x.fini()
|
||||
|
||||
// ❌ 以下は全てエラー
|
||||
x.someMethod() // → "Instance was finalized; further use is prohibited"
|
||||
x.field = value // → 同上
|
||||
local val = x.field // → 同上
|
||||
```
|
||||
|
||||
## 🏗️ 実装アーキテクチャ
|
||||
|
||||
### InstanceBox拡張
|
||||
```rust
|
||||
pub struct InstanceBox {
|
||||
// 既存フィールド...
|
||||
|
||||
init_field_order: Vec<String>, // 決定的カスケード順序
|
||||
weak_fields_union: HashSet<String>, // weak判定高速化
|
||||
in_finalization: bool, // 再入防止
|
||||
finalized: bool, // 使用禁止フラグ
|
||||
}
|
||||
```
|
||||
|
||||
### 実行時ガード
|
||||
- **メソッド呼び出し**: `finalized`チェック → エラー
|
||||
- **フィールドアクセス**: `finalized`チェック → エラー
|
||||
- **フィールド代入**: `finalized`チェック → エラー
|
||||
|
||||
## 💡 使用例とパターン
|
||||
|
||||
### 基本的な使用例
|
||||
```nyash
|
||||
box FileHandler {
|
||||
init { filename, handle }
|
||||
|
||||
pack(name) {
|
||||
me.filename = name
|
||||
me.handle = openFile(name)
|
||||
}
|
||||
|
||||
fini() {
|
||||
if (me.handle != null) {
|
||||
closeFile(me.handle)
|
||||
print("File " + me.filename + " closed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
local handler = new FileHandler("data.txt")
|
||||
// ... ファイル操作 ...
|
||||
handler.fini() // 明示的クリーンアップ
|
||||
```
|
||||
|
||||
### 再代入時の自動解放
|
||||
```nyash
|
||||
box Holder { init { resource } }
|
||||
box Resource { fini() { print("Resource cleaned up") } }
|
||||
|
||||
local h = new Holder()
|
||||
h.resource = new Resource() // 新しいリソース設定
|
||||
h.resource = new Resource() // → 前のリソースが自動的にfini()される
|
||||
```
|
||||
|
||||
### カスタム解放順序
|
||||
```nyash
|
||||
box DatabaseConnection {
|
||||
init { transaction, connection, logger }
|
||||
|
||||
fini() {
|
||||
// 依存関係に基づく手動順序制御
|
||||
if (me.transaction != null) {
|
||||
me.transaction.rollback()
|
||||
me.transaction.fini()
|
||||
}
|
||||
|
||||
if (me.connection != null) {
|
||||
me.connection.close()
|
||||
me.connection.fini()
|
||||
}
|
||||
|
||||
// loggerは自動カスケードに任せる
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 循環参照の安全な解決
|
||||
```nyash
|
||||
box Node {
|
||||
init { data, weak parent, children }
|
||||
|
||||
pack(value) {
|
||||
me.data = value
|
||||
me.children = new ArrayBox()
|
||||
}
|
||||
|
||||
addChild(child) {
|
||||
me.children.push(child)
|
||||
child.setParent(me) // 子→親はweak参照
|
||||
}
|
||||
|
||||
fini() {
|
||||
// 子ノードを先に解放
|
||||
loop (me.children.length() > 0) {
|
||||
local child = me.children.pop()
|
||||
child.fini()
|
||||
}
|
||||
// 親への参照は自動的にnil化される
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 🧪 テストパターン
|
||||
|
||||
### 基本動作テスト
|
||||
```nyash
|
||||
box Counter {
|
||||
init { value }
|
||||
pack() { me.value = 0 }
|
||||
increment() { me.value = me.value + 1 }
|
||||
fini() { print("Counter finalized with value: " + me.value.toString()) }
|
||||
}
|
||||
|
||||
local c = new Counter()
|
||||
c.increment()
|
||||
c.increment()
|
||||
c.fini()
|
||||
// c.increment() // → エラー: finalized後の使用禁止
|
||||
```
|
||||
|
||||
### 循環参照テスト
|
||||
```nyash
|
||||
box Parent {
|
||||
init { child }
|
||||
pack() {
|
||||
me.child = new Child()
|
||||
me.child.setParent(me)
|
||||
}
|
||||
fini() { print("Parent finalized") }
|
||||
}
|
||||
|
||||
box Child {
|
||||
init { weak parent }
|
||||
setParent(p) { me.parent = p }
|
||||
fini() { print("Child finalized") }
|
||||
}
|
||||
|
||||
local p = new Parent()
|
||||
p.fini() // Parent → Child の順で解放、リークなし
|
||||
```
|
||||
|
||||
## 🎯 期待される効果
|
||||
|
||||
### メモリ安全性
|
||||
- **循環参照リーク完全防止**: weak参照とfiniの組み合わせ
|
||||
- **二重解放防止**: idempotentな設計
|
||||
- **使用禁止ガード**: finalized後の誤用防止
|
||||
|
||||
### 予測可能性
|
||||
- **決定的順序**: init宣言順による自動カスケード
|
||||
- **明示的制御**: ユーザー定義fini()での柔軟な順序指定
|
||||
- **エラーメッセージ**: 明確で修正提案付きのエラー
|
||||
|
||||
### 開発体験
|
||||
- **直感的**: リソースの「終了宣言」として理解しやすい
|
||||
- **デバッグ容易**: 解放タイミングが明確
|
||||
- **保守性**: 依存関係の変更に強い設計
|
||||
|
||||
## 📚 関連ドキュメント
|
||||
|
||||
- [weak参照設計](weak-reference-design.md) - 循環参照解決との統合
|
||||
- [Everything is Box](design-philosophy.md) - 基本設計思想
|
||||
- [言語リファレンス](language-reference.md) - 構文詳細
|
||||
|
||||
---
|
||||
|
||||
**Everything is Box, Everything is Finalized!** 🔥
|
||||
@ -1,525 +0,0 @@
|
||||
# 🚀 Nyash Language Reference 2025
|
||||
|
||||
**最終更新: 2025年8月11日 - デリゲーション革命完了!`from`統一構文+`init`構文決定!**
|
||||
|
||||
## 📖 概要
|
||||
|
||||
Nyashは「Everything is Box」哲学に基づく革新的プログラミング言語です。
|
||||
Rust製インタープリターによる高性能実行と、直感的な構文により、学習しやすく実用的な言語として完成しました。
|
||||
|
||||
---
|
||||
|
||||
## 🔤 **1. 予約語・キーワード完全リスト**
|
||||
|
||||
### **コア言語**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `box` | クラス定義 | `box MyClass { }` |
|
||||
| `static` | 静的Box・関数定義 | `static box Main { }` |
|
||||
| `interface` | インターフェース定義 | `interface Comparable { }` |
|
||||
| `from` | デリゲーション指定 | `box Child from Parent { }` |
|
||||
| `new` | オブジェクト生成 | `new ConsoleBox()` |
|
||||
| `me`/`this` | 自己参照 | `me.field = value` |
|
||||
|
||||
### **変数・スコープ**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `local` | ローカル変数宣言 | `local x, y = 10` |
|
||||
| `outbox` | 所有権移転変数 | `outbox result = compute()` |
|
||||
| `global` | グローバル変数 | `global CONFIG = "dev"` |
|
||||
| `init` | フィールド初期化ブロック | `init { name, age }` |
|
||||
|
||||
### **制御構文**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `if` | 条件分岐 | `if condition { }` |
|
||||
| `else` | else節 | `else { }` |
|
||||
| `loop` | ループ(唯一の形式) | `loop(condition) { }` |
|
||||
| `break` | ループ脱出 | `break` |
|
||||
| `return` | 関数リターン | `return value` |
|
||||
|
||||
### **論理・演算**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `not` | 論理否定 | `not condition` |
|
||||
| `and` | 論理積 | `a and b` |
|
||||
| `or` | 論理和 | `a or b` |
|
||||
| `true`/`false` | 真偽値 | `flag = true` |
|
||||
|
||||
### **非同期・並行**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `nowait` | 非同期実行 | `nowait future = task()` |
|
||||
| `await` | 待機・結果取得 | `result = await future` |
|
||||
|
||||
### **例外処理**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `try` | 例外捕獲開始 | `try { }` |
|
||||
| `catch` | 例外処理 | `catch (e) { }` |
|
||||
| `finally` | 最終処理 | `finally { }` |
|
||||
| `throw` | 例外発生 | `throw error` |
|
||||
|
||||
### **その他**
|
||||
| 予約語 | 用途 | 例 |
|
||||
|-------|------|---|
|
||||
| `function` | 関数定義 | `function add(a,b) { }` |
|
||||
| `print` | 出力 | `print("Hello")` |
|
||||
| `include` | ファイル取り込み | `include "math.nyash"` |
|
||||
|
||||
---
|
||||
|
||||
## 📝 **2. 文法・構文仕様**
|
||||
|
||||
### **2.1 Box定義文法**
|
||||
|
||||
#### **基本Box**
|
||||
```nyash
|
||||
box ClassName {
|
||||
init { field1, field2, field3 } # カンマ必須!CPU暴走防止
|
||||
|
||||
# コンストラクタ
|
||||
init(param1, param2) { # init構文に統一
|
||||
me.field1 = param1
|
||||
me.field2 = param2
|
||||
me.field3 = defaultValue()
|
||||
}
|
||||
|
||||
# メソッド
|
||||
methodName(arg1, arg2) {
|
||||
return me.field1 + arg1
|
||||
}
|
||||
|
||||
# デストラクタ
|
||||
fini() {
|
||||
print("Cleanup: " + me.field1)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### **デリゲーションBox**
|
||||
```nyash
|
||||
box Child from Parent interface Comparable {
|
||||
init { childField }
|
||||
|
||||
init(parentParam, childParam) { # init構文に統一
|
||||
from Parent.init(parentParam) # 親コンストラクタ明示呼び出し
|
||||
me.childField = childParam
|
||||
}
|
||||
|
||||
# メソッドオーバーライド
|
||||
override process(data) { # override必須
|
||||
local result = from Parent.process(data) # 親メソッド呼び出し
|
||||
return result + " (Child processed)"
|
||||
}
|
||||
|
||||
# インターフェース実装
|
||||
compareTo(other) {
|
||||
return me.value - other.value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### **Static Box(推奨エントリーポイント)**
|
||||
```nyash
|
||||
static box Main {
|
||||
init { console, result }
|
||||
|
||||
main() {
|
||||
me.console = new ConsoleBox()
|
||||
me.console.log("🎉 Everything is Box!")
|
||||
return "Success"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### **ジェネリックBox**
|
||||
```nyash
|
||||
box Container<T> {
|
||||
init { value }
|
||||
|
||||
Container(item) {
|
||||
me.value = item
|
||||
}
|
||||
|
||||
getValue() {
|
||||
return me.value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **2.2 変数宣言**
|
||||
|
||||
#### **基本パターン**
|
||||
```nyash
|
||||
# 単一宣言
|
||||
local x
|
||||
local name = "初期値"
|
||||
|
||||
# 複数宣言
|
||||
local a, b, c
|
||||
local x = 10, y = 20, z # 混合初期化
|
||||
|
||||
# 所有権移転(static関数内)
|
||||
static function Factory.create() {
|
||||
outbox product # 呼び出し側に所有権移転
|
||||
product = new Item()
|
||||
return product
|
||||
}
|
||||
```
|
||||
|
||||
#### **変数宣言厳密化システム(2025-08-09実装)**
|
||||
```nyash
|
||||
# ✅ 正しい - 明示宣言必須
|
||||
local temp
|
||||
temp = 42
|
||||
|
||||
# ❌ エラー - 未宣言変数への代入
|
||||
x = 42 # RuntimeError: 未宣言変数 + 修正提案表示
|
||||
```
|
||||
|
||||
### **2.3 制御構文**
|
||||
|
||||
#### **条件分岐**
|
||||
```nyash
|
||||
if condition {
|
||||
# 処理
|
||||
} else if condition2 {
|
||||
# 処理2
|
||||
} else {
|
||||
# else処理
|
||||
}
|
||||
```
|
||||
|
||||
#### **ループ(統一構文)**
|
||||
```nyash
|
||||
# ✅ 唯一の正しい形式
|
||||
loop(condition) {
|
||||
# ループ本体
|
||||
if exitCondition {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
# ❌ 削除済み - 使用不可
|
||||
while condition { } # パーサーエラー
|
||||
loop() { } # パーサーエラー
|
||||
```
|
||||
|
||||
### **2.4 演算子・式**
|
||||
|
||||
#### **🚀 新実装: 関数オーバーロードシステム**
|
||||
```nyash
|
||||
# Rust風トレイトベース演算子(2025-08-10実装完了)
|
||||
sum = 10 + 20 # IntegerBox + IntegerBox = IntegerBox
|
||||
concat = "Hi" + " !" # StringBox + StringBox = StringBox
|
||||
repeat = "Ha" * 3 # StringBox * IntegerBox = "HaHaHa"
|
||||
mixed = 42 + " answer" # 混合型 → 自動文字列結合フォールバック
|
||||
```
|
||||
|
||||
#### **演算子優先順位**
|
||||
```nyash
|
||||
result = a + b * c / d - e # 算術演算子は標準的優先順位
|
||||
logic = not a and b or c # not > and > or
|
||||
compare = (x > y) and (z <= w) # 比較は括弧推奨
|
||||
```
|
||||
|
||||
#### **論理演算子**
|
||||
```nyash
|
||||
# キーワード版(推奨)
|
||||
canAccess = level >= 5 and hasKey
|
||||
isValid = not (isEmpty or hasError)
|
||||
|
||||
# シンボル版(互換)
|
||||
result = condition && other || fallback # 利用可能だが非推奨
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🏗️ **3. Box構文詳細ガイド**
|
||||
|
||||
### **3.1 Everything is Box 原則**
|
||||
|
||||
```nyash
|
||||
# すべての値がBox
|
||||
number = 42 # IntegerBox
|
||||
text = "hello" # StringBox
|
||||
flag = true # BoolBox
|
||||
array = new ArrayBox() # ArrayBox
|
||||
console = new ConsoleBox() # ConsoleBox
|
||||
|
||||
# 統一的なメソッド呼び出し
|
||||
print(number.to_string_box().value) # "42"
|
||||
print(array.length()) # 配列長
|
||||
console.log("Everything is Box!") # コンソール出力
|
||||
```
|
||||
|
||||
### **3.2 コンストラクタパターン**
|
||||
|
||||
#### **パラメータ付きコンストラクタ**
|
||||
```nyash
|
||||
box Person {
|
||||
init { name, age, email }
|
||||
|
||||
init(personName, personAge) { # init構文に統一
|
||||
me.name = personName
|
||||
me.age = personAge
|
||||
me.email = me.name + "@example.com" # 計算フィールド
|
||||
}
|
||||
|
||||
# ファクトリーメソッド
|
||||
static createGuest() {
|
||||
outbox guest
|
||||
guest = new Person("Guest", 0)
|
||||
return guest
|
||||
}
|
||||
}
|
||||
|
||||
# 使用例
|
||||
person = new Person("Alice", 25)
|
||||
guest = Person.createGuest()
|
||||
```
|
||||
|
||||
### **3.3 継承とインターフェース**
|
||||
|
||||
#### **デリゲーションチェーン**
|
||||
```nyash
|
||||
# 基底Box
|
||||
box Animal {
|
||||
init { name, species }
|
||||
|
||||
init(animalName, animalSpecies) {
|
||||
me.name = animalName
|
||||
me.species = animalSpecies
|
||||
}
|
||||
|
||||
speak() {
|
||||
return me.name + " makes a sound"
|
||||
}
|
||||
}
|
||||
|
||||
# デリゲーション
|
||||
box Dog from Animal {
|
||||
init { breed } # 追加フィールド
|
||||
|
||||
init(dogName, dogBreed) {
|
||||
from Animal.init(dogName, "Canine") # 親コンストラクタ呼び出し
|
||||
me.breed = dogBreed
|
||||
}
|
||||
|
||||
override speak() { # 明示的オーバーライド
|
||||
return me.name + " barks: Woof!"
|
||||
}
|
||||
}
|
||||
|
||||
# インターフェース実装
|
||||
box Cat from Animal interface Playful {
|
||||
# Playfulインターフェースの実装必須
|
||||
}
|
||||
```
|
||||
|
||||
### **3.4 Static Boxパターン**
|
||||
|
||||
#### **名前空間・ユーティリティ**
|
||||
```nyash
|
||||
static box MathUtils {
|
||||
init { PI, E }
|
||||
|
||||
static {
|
||||
me.PI = 3.14159265
|
||||
me.E = 2.71828182
|
||||
}
|
||||
|
||||
add(a, b) {
|
||||
return a + b
|
||||
}
|
||||
|
||||
circleArea(radius) {
|
||||
return me.PI * radius * radius
|
||||
}
|
||||
}
|
||||
|
||||
# 使用法
|
||||
area = MathUtils.circleArea(5)
|
||||
sum = MathUtils.add(10, 20)
|
||||
pi = MathUtils.PI
|
||||
```
|
||||
|
||||
#### **アプリケーションエントリーポイント**
|
||||
```nyash
|
||||
# 🎯 推奨: Static Box Main パターン
|
||||
static box Main {
|
||||
init { console, result }
|
||||
|
||||
main() {
|
||||
me.console = new ConsoleBox()
|
||||
me.console.log("🚀 Starting application...")
|
||||
|
||||
# アプリケーションロジック
|
||||
me.result = processData()
|
||||
|
||||
return "Application completed successfully"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🚀 **4. 最新機能・革新技術**
|
||||
|
||||
### **4.1 Arc<Mutex> Revolution(2025-08-10)**
|
||||
```nyash
|
||||
# 全16種類のBox型が統一Arc<Mutex>パターンで実装
|
||||
# 完全なスレッドセーフティと高性能を両立
|
||||
|
||||
array = new ArrayBox()
|
||||
array.push(10) # スレッドセーフな追加
|
||||
array.push(20)
|
||||
item = array.get(0) # スレッドセーフな取得
|
||||
|
||||
json = new JSONBox()
|
||||
json.set("name", "Alice") # 並行安全な操作
|
||||
data = json.stringify() # JSON文字列化
|
||||
```
|
||||
|
||||
### **4.2 Rust風トレイトベース演算子(2025-08-10)**
|
||||
```nyash
|
||||
# AI大相談会で決定された最適設計
|
||||
# 静的・動的ハイブリッドディスパッチによる高性能実現
|
||||
|
||||
# 整数演算
|
||||
result = 100 - 25 # IntegerBox間演算 → IntegerBox
|
||||
product = 6 * 7 # 高速静的ディスパッチ
|
||||
|
||||
# 文字列操作
|
||||
greeting = "Hello" + " World" # 文字列結合
|
||||
repeated = "Echo" * 3 # "EchoEchoEcho"
|
||||
|
||||
# 混合型フォールバック
|
||||
message = "Answer: " + 42 # "Answer: 42"
|
||||
|
||||
# Boolean演算
|
||||
boolSum = true + false # 1 (IntegerBox)
|
||||
```
|
||||
|
||||
### **4.3 変数宣言厳密化(2025-08-09)**
|
||||
```nyash
|
||||
# メモリ安全性・非同期安全性保証システム
|
||||
|
||||
static box Calculator {
|
||||
init { memory } # 必須フィールド宣言
|
||||
|
||||
calculate() {
|
||||
local temp # 必須ローカル変数宣言
|
||||
temp = me.memory * 2
|
||||
return temp
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚡ **5. 実装済みBox型ライブラリ**
|
||||
|
||||
### **5.1 基本型**
|
||||
- `StringBox` - 文字列(split, find, replace, trim等)
|
||||
- `IntegerBox` - 64bit整数
|
||||
- `BoolBox` - 真偽値
|
||||
- `VoidBox` - null/void値
|
||||
|
||||
### **5.2 コレクション**
|
||||
- `ArrayBox` - 動的配列(push, pop, get, set, join等)
|
||||
- `MapBox` - 連想配列・辞書
|
||||
|
||||
### **5.3 システム・I/O**
|
||||
- `ConsoleBox` - コンソール入出力
|
||||
- `DebugBox` - デバッグ支援・メモリ追跡
|
||||
- `FileBox` - ファイルシステム操作
|
||||
|
||||
### **5.4 数学・時間**
|
||||
- `MathBox` - 数学関数(sin, cos, log, sqrt等)
|
||||
- `TimeBox` - 時刻操作・タイマー
|
||||
- `RandomBox` - 乱数生成・選択・シャッフル
|
||||
|
||||
### **5.5 データ処理**
|
||||
- `JSONBox` - JSON解析・生成(parse, stringify, get, set)
|
||||
- `RegexBox` - 正規表現(test, find, replace, split)
|
||||
- `BufferBox` - バイナリデータ処理
|
||||
- `StreamBox` - ストリーム処理
|
||||
|
||||
### **5.6 ネットワーク・Web**
|
||||
- `HttpClientBox` - HTTP通信
|
||||
- `WebDisplayBox` - HTML表示(WASM)
|
||||
- `WebConsoleBox` - ブラウザコンソール(WASM)
|
||||
- `WebCanvasBox` - Canvas描画(WASM)
|
||||
|
||||
### **5.7 GUI・マルチメディア**
|
||||
- `EguiBox` - デスクトップGUI(Windows/Linux)
|
||||
- `SoundBox` - 音声再生
|
||||
|
||||
---
|
||||
|
||||
## 🎯 **6. パフォーマンス・デザイン原則**
|
||||
|
||||
### **6.1 メモリ安全性**
|
||||
- Rust所有権システムによる完全なメモリ安全性
|
||||
- Arc<Mutex>によるスレッドセーフな共有状態管理
|
||||
- 自動参照カウント + 明示的デストラクタ(fini)
|
||||
|
||||
### **6.2 実行効率**
|
||||
- 統一されたBox型システムによる最適化
|
||||
- 静的・動的ハイブリッドディスパッチで高速演算
|
||||
- パーサー無限ループ対策(--debug-fuel)
|
||||
|
||||
### **6.3 開発効率**
|
||||
- 変数宣言厳密化による早期エラー検出
|
||||
- 包括的デバッグ機能(DebugBox)
|
||||
- 直感的な"Everything is Box"概念
|
||||
|
||||
---
|
||||
|
||||
## 📚 **7. 学習パス・ベストプラクティス**
|
||||
|
||||
### **7.1 初心者向け学習順序**
|
||||
1. **基本概念**: Everything is Box哲学理解
|
||||
2. **基本構文**: 変数宣言・制御構文・演算子
|
||||
3. **Box定義**: 基本的なクラス作成
|
||||
4. **Static Box Main**: アプリケーションエントリーポイント
|
||||
5. **継承・インターフェース**: オブジェクト指向機能
|
||||
|
||||
### **7.2 推奨コーディングスタイル**
|
||||
```nyash
|
||||
# ✅ 推奨スタイル
|
||||
static box Main {
|
||||
init { console, result } # フィールド明示
|
||||
|
||||
main() {
|
||||
me.console = new ConsoleBox()
|
||||
|
||||
local data # 変数事前宣言
|
||||
data = processInput()
|
||||
|
||||
me.result = data # 明確な代入
|
||||
return "Success"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### **7.3 よくある間違いと対策**
|
||||
```nyash
|
||||
# ❌ よくある間違い
|
||||
init { field1 field2 } # カンマなし → CPU暴走
|
||||
x = 42 # 変数未宣言 → ランタイムエラー
|
||||
while condition { } # 非対応構文 → パーサーエラー
|
||||
|
||||
# ✅ 正しい書き方
|
||||
init { field1, field2 } # カンマ必須
|
||||
local x = 42 # 事前宣言
|
||||
loop(condition) { } # 統一ループ構文
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**🎉 Nyash 2025は、AI協働設計による最先端言語システムとして、シンプルさと強力さを完全に両立しました。**
|
||||
|
||||
*最終更新: 2025年8月10日 - Arc<Mutex> Revolution + AI大相談会成功記念*
|
||||
@ -1,338 +0,0 @@
|
||||
# 🌟 Nyash Override + Delegation 統一構文仕様
|
||||
|
||||
バージョン: 2.0
|
||||
作成日: 2025年8月11日
|
||||
ステータス: 正式決定
|
||||
|
||||
## 📋 概要
|
||||
|
||||
Nyashプログラミング言語における明示的オーバーライドとデリゲーション構文の完全仕様。世界初の**完全明示デリゲーション言語**としてのNyashの核心機能を定義する。
|
||||
|
||||
## 🎯 設計哲学
|
||||
|
||||
### 基本原則
|
||||
1. **完全明示性**: すべての動作を明示的に宣言
|
||||
2. **曖昧性の完全排除**: 暗黙の動作は一切許可しない
|
||||
3. **統一構文**: デリゲーションとオーバーライドの完全統合
|
||||
4. **初学者フレンドリー**: 直感的で理解しやすい構文
|
||||
|
||||
### Everything is Box との整合性
|
||||
- すべてのオブジェクトがBox
|
||||
- デリゲーション先もBox
|
||||
- オーバーライドもBoxメソッドの置換
|
||||
|
||||
## 🔥 基本構文
|
||||
|
||||
### デリゲーション宣言
|
||||
```nyash
|
||||
box Child from Parent {
|
||||
// 親Boxからの機能デリゲーション
|
||||
}
|
||||
|
||||
// 多重デリゲーション
|
||||
box Child from Parent1, Parent2 {
|
||||
// 複数のBoxからの機能デリゲーション
|
||||
}
|
||||
```
|
||||
|
||||
### メソッドオーバーライド
|
||||
```nyash
|
||||
box Child from Parent {
|
||||
// 必須: overrideキーワードによる明示的宣言
|
||||
override methodName(params) {
|
||||
// オーバーライド実装
|
||||
from Parent.methodName(params) // 親実装呼び出し(任意)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### コンストラクタデリゲーション
|
||||
```nyash
|
||||
box Child from Parent {
|
||||
init(params) { # init構文に統一
|
||||
from Parent.init(params) # 必須: 親コンストラクタ明示呼び出し
|
||||
me.childSpecificField = value
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 📚 詳細仕様
|
||||
|
||||
### 1. Override キーワード
|
||||
|
||||
#### 必須条件
|
||||
- 親Box(デリゲーション先)に同名メソッドが存在する場合のみ使用可能
|
||||
- 同名メソッドが存在しない場合は**コンパイルエラー**
|
||||
|
||||
#### 禁止事項
|
||||
- 同一Box内での同名メソッド重複定義は**すべてエラー**
|
||||
- 暗黙のオーバーライドは**完全禁止**
|
||||
|
||||
#### 構文例
|
||||
```nyash
|
||||
box MeshNode from P2PBox {
|
||||
// ✅ 正しい使用法
|
||||
override send(intent, data, target) {
|
||||
me.routing.log(target)
|
||||
from P2PBox.send(intent, data, target)
|
||||
}
|
||||
|
||||
// ❌ エラー: P2PBoxに存在しないメソッド
|
||||
override nonExistentMethod() {
|
||||
// Error: Method 'nonExistentMethod' does not exist in parent P2PBox
|
||||
}
|
||||
|
||||
// ❌ エラー: overrideなしで親メソッドと同名
|
||||
send(intent, data, target) {
|
||||
// Error: Method 'send' overrides parent method. Add 'override' keyword.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. From デリゲーション構文
|
||||
|
||||
#### 基本構文
|
||||
```nyash
|
||||
from ParentBox.methodName(args)
|
||||
from ParentBox.fieldName
|
||||
```
|
||||
|
||||
#### メソッド呼び出し
|
||||
```nyash
|
||||
// 親の特定メソッドを明示的に呼び出し
|
||||
from P2PBox.send(intent, data, target)
|
||||
|
||||
// 複数親からの呼び出し
|
||||
from Logger.log("Starting operation")
|
||||
from P2PBox.send(intent, data, target)
|
||||
from Cache.store(data)
|
||||
```
|
||||
|
||||
#### フィールドアクセス
|
||||
```nyash
|
||||
// 親のフィールドへのアクセス
|
||||
local status = from P2PBox.connectionStatus
|
||||
from Logger.logLevel = "DEBUG"
|
||||
```
|
||||
|
||||
#### コンストラクタ呼び出し
|
||||
```nyash
|
||||
init(nodeId, world) { # init構文に統一
|
||||
from P2PBox.init(nodeId, world) # 完全統一構文
|
||||
me.routing = RoutingTable()
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 多重デリゲーション
|
||||
|
||||
#### 基本形式
|
||||
```nyash
|
||||
box ComplexNode from P2PBox, Logger, Cache {
|
||||
override send(intent, data, target) {
|
||||
from Logger.debug("Sending: " + intent) // Logger親から
|
||||
from Cache.store(intent, data) // Cache親から
|
||||
from P2PBox.send(intent, data, target) // P2PBox親から
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 曖昧性の解消
|
||||
```nyash
|
||||
// 複数親に同名メソッドが存在する場合
|
||||
box ConflictNode from ParentA, ParentB {
|
||||
// ❌ エラー: どちらのprocessを置換するか不明
|
||||
override process(data) {
|
||||
// Error: Method 'process' exists in multiple parents. Use specific parent.
|
||||
}
|
||||
|
||||
// ✅ 正しい解決法: 親を明示指定
|
||||
override ParentA.process(data) {
|
||||
// ParentAのprocessをオーバーライド
|
||||
from ParentA.process(data)
|
||||
}
|
||||
|
||||
override ParentB.process(data) {
|
||||
// ParentBのprocessをオーバーライド
|
||||
from ParentB.process(data)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 4. エラーメッセージ仕様
|
||||
|
||||
#### 重複定義エラー
|
||||
```
|
||||
Error: Method 'send' is already defined in this box at line 15.
|
||||
--> box.nyash:20:5
|
||||
|
|
||||
20 | send(msg) {
|
||||
| ^^^^ duplicate method definition
|
||||
|
|
||||
Help: Remove duplicate definition or rename method.
|
||||
```
|
||||
|
||||
#### Missing Override エラー
|
||||
```
|
||||
Error: Method 'send' overrides a parent method. Add 'override' keyword.
|
||||
--> box.nyash:18:5
|
||||
|
|
||||
18 | send(intent, data, target) {
|
||||
| ^^^^ missing 'override' keyword
|
||||
|
|
||||
Help: Change to 'override send(intent, data, target) {'
|
||||
```
|
||||
|
||||
#### Wrong Override エラー
|
||||
```
|
||||
Error: Method 'newMethod' does not exist in any parent. Remove 'override' keyword.
|
||||
--> box.nyash:22:5
|
||||
|
|
||||
22 | override newMethod() {
|
||||
| ^^^^^^^^ unnecessary 'override'
|
||||
|
|
||||
Help: Remove 'override' or verify parent method name.
|
||||
```
|
||||
|
||||
#### 曖昧Override エラー
|
||||
```
|
||||
Error: Method 'process' exists in multiple parents. Specify which parent to override.
|
||||
--> box.nyash:25:5
|
||||
|
|
||||
25 | override process(data) {
|
||||
| ^^^^^^^^ ambiguous override
|
||||
|
|
||||
Help: Use 'override ParentA.process' or 'override ParentB.process'
|
||||
```
|
||||
|
||||
## 🚫 禁止事項
|
||||
|
||||
### 1. 暗黙のオーバーライド
|
||||
```nyash
|
||||
box Child from Parent {
|
||||
send(msg) { // ❌ エラー: overrideキーワードなし
|
||||
print("Child implementation")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. コンストラクタオーバーロード
|
||||
```nyash
|
||||
box Node {
|
||||
init(id) { // 最初の定義
|
||||
me.id = id
|
||||
}
|
||||
|
||||
init(id, name) { // ❌ エラー: 重複定義
|
||||
me.id = id
|
||||
me.name = name
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. 同名メソッド重複定義
|
||||
```nyash
|
||||
box Example {
|
||||
process(data) { // 最初の定義
|
||||
print("Version 1")
|
||||
}
|
||||
|
||||
process(data) { // ❌ エラー: 重複定義
|
||||
print("Version 2")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## ✅ ベストプラクティス
|
||||
|
||||
### 1. 明示的な親呼び出し
|
||||
```nyash
|
||||
box MeshNode from P2PBox {
|
||||
override send(intent, data, target) {
|
||||
// 前処理
|
||||
me.routing.logOutgoing(target)
|
||||
|
||||
// 親実装呼び出し(明示的)
|
||||
from P2PBox.send(intent, data, target)
|
||||
|
||||
// 後処理
|
||||
me.statistics.incrementSentCount()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. 多重デリゲーションでの順序指定
|
||||
```nyash
|
||||
box SmartNode from P2PBox, Logger, Cache {
|
||||
override send(intent, data, target) {
|
||||
// 1. ログ記録
|
||||
from Logger.info("Sending to: " + target)
|
||||
|
||||
// 2. キャッシュ保存
|
||||
from Cache.store(intent + ":" + target, data)
|
||||
|
||||
// 3. 実際の送信
|
||||
from P2PBox.send(intent, data, target)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 3. コンストラクタチェーン
|
||||
```nyash
|
||||
box SecureNode from P2PBox {
|
||||
init security = SecurityManager()
|
||||
|
||||
init(nodeId, world, keyFile) { # init構文に統一
|
||||
// 1. 親初期化(必須)
|
||||
from P2PBox.init(nodeId, world)
|
||||
|
||||
// 2. 子固有の初期化
|
||||
me.security = SecurityManager()
|
||||
me.security.loadKeys(keyFile)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 🔮 将来の拡張
|
||||
|
||||
### 1. Final メソッド(検討中)
|
||||
```nyash
|
||||
box Parent {
|
||||
final criticalMethod() { // オーバーライド禁止
|
||||
// 重要な処理
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Abstract メソッド(検討中)
|
||||
```nyash
|
||||
box AbstractParent {
|
||||
abstract process(data) // 子でのoverride必須
|
||||
}
|
||||
```
|
||||
|
||||
### 3. Override チェック強化(検討中)
|
||||
```nyash
|
||||
override! send(data) { // 親呼び出し必須チェック
|
||||
// from Parent.send(data) がないとエラー
|
||||
}
|
||||
```
|
||||
|
||||
## 📊 他言語との比較
|
||||
|
||||
| 言語 | 継承方式 | オーバーライド | 親呼び出し | 多重継承 |
|
||||
|------|----------|---------------|-----------|----------|
|
||||
| **Nyash** | デリゲーション | `override` 必須 | `from Parent.method()` | 明示的解消 |
|
||||
| Java | クラス継承 | `@Override` 注釈 | `super.method()` | 不可 |
|
||||
| Python | クラス継承 | 暗黙 | `super().method()` | MRO(複雑) |
|
||||
| C# | クラス継承 | `override` 必須 | `base.method()` | 不可 |
|
||||
| TypeScript | プロトタイプ | 暗黙 | `super.method()` | 不可 |
|
||||
|
||||
### Nyashの優位性
|
||||
1. **完全な明示性**: すべての動作が明確
|
||||
2. **曖昧性の完全排除**: 多重デリゲーションでも安全
|
||||
3. **統一構文**: デリゲーションとオーバーライドが統合
|
||||
4. **初学者フレンドリー**: 分かりやすいエラーメッセージ
|
||||
|
||||
---
|
||||
|
||||
**この仕様により、Nyashは世界初の「完全明示デリゲーション言語」として、プログラミング言語史に新たな1ページを刻むことになります。** 🌟
|
||||
Reference in New Issue
Block a user