From 06a875e67186f6dbf0d7cd9feeff4228c60bb61d Mon Sep 17 00:00:00 2001 From: usbharu <i@usbharu.dev> Date: Tue, 28 Jan 2025 23:48:16 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/posts/2025-01-27/index.md | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/content/posts/2025-01-27/index.md b/content/posts/2025-01-27/index.md index 09ea91a..cee4bae 100644 --- a/content/posts/2025-01-27/index.md +++ b/content/posts/2025-01-27/index.md @@ -3,7 +3,7 @@ author: usbharu draft: false categories: - 技術 -date: 2025-01-12T14:08:58+09:00 +date: 2025-01-27T14:08:58+09:00 tags: - Kotlin - ActivityPub @@ -17,11 +17,13 @@ keywords: - ActivityStreams - ActivityVocabulary title: Kotlinで作るDSL -relpermalink: posts/2025-01-12/ -url: posts/2025-01-12/ +relpermalink: posts/2025-01-27/ +url: posts/2025-01-27/ decription: KotlinでデータとDSLが分離されたタイプのDSLの作成 --- +[usbharu/kotlin-dsl-demo](https://github.com/usbharu/kotlin-dsl-demo) + KotlinでひたすらDSLを書いてたらだいぶノウハウが溜まってきたので共有 KotlinのDSL作成でググるとこんな記事が出てくると思いますが、大半の人は下の記事を読むだけで作りたいものが作れると思うのでぜひ読んでください。 @@ -44,7 +46,7 @@ KotlinのDSL作成でググるとこんな記事が出てくると思います 逆にデメリットは - 単純に考えて必要なクラス数が増える(どこまで凝るかによるが2倍以上) - それに伴う保守性の低下 -- Kotlinのうんこ仕様によるプロパティの露出 +- プロパティの露出 ### メリット @@ -81,4 +83,19 @@ val buildString = buildString { append(i) } } -``` \ No newline at end of file +``` + +### デメリット + +#### 必要なクラス数が増える & 保守性の低下 + +データのクラスとそれに対応するBuilderというかDSLの分増えるので雑に計算すると2倍です。共通化できる部分もあるのでもうちょっと少ない場合もあるかも。それに伴って保守性も低下します。 + +#### プロパティの露出 + +デフォルト実装作ろうとするとinterfaceはprotectedにできないしpackage-privateも無いのでpublicにせざるを得ず、触ってほしくないプロパティまで露出してしまいます。一応internalはありますが、プロジェクト内で使うDSLとかだと微妙だし~~[そもそもinternalは無視できる](https://stackoverflow.com/questions/62500464/is-it-possible-to-access-internal-class-with-reflection-in-kotlin)~~ + + +というわけで色々実践してみたリポジトリ + +[usbharu/kotlin-dsl-demo](https://github.com/usbharu/kotlin-dsl-demo) \ No newline at end of file