This commit is contained in:
usbharu 2025-01-28 23:48:16 +09:00
parent 7dfeb568d8
commit 06a875e671
Signed by: usbharu
GPG Key ID: 95CBCF7046307B77
1 changed files with 22 additions and 5 deletions
content/posts/2025-01-27

View File

@ -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)
}
}
```
```
### デメリット
#### 必要なクラス数が増える & 保守性の低下
データのクラスとそれに対応する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)