https://github.com/cjstehno/polyglot
Some Kotlin code that provides DSL-like support for Kotlin, Java and Groovy without loss of functionalilty.
https://github.com/cjstehno/polyglot
groovy java kotlin
Last synced: 3 months ago
JSON representation
Some Kotlin code that provides DSL-like support for Kotlin, Java and Groovy without loss of functionalilty.
- Host: GitHub
- URL: https://github.com/cjstehno/polyglot
- Owner: cjstehno
- License: other
- Created: 2017-10-31T16:32:09.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-07T14:33:13.000Z (over 8 years ago)
- Last Synced: 2025-03-11T07:31:48.544Z (over 1 year ago)
- Topics: groovy, java, kotlin
- Language: Kotlin
- Size: 60.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license_header.txt
Awesome Lists containing this project
README
# Polyglot DSL
Simple project to test an idea I had about using Kotlin to develop a DSL which can be made painlessly available to Kotlin, Java and Groovy.
> Note: I am fairly new to Kotlin, so I make no promises that this is actually _good_ Kotlin code, but it _does_ work.
The goal was to provide a common code-base and configuration objects, but then allow for more customized configuration methods (DSL-style) as available to the languages.
## Kotlin
Since the base code is written in Kotlin, the Kotlin interface is pretty straight-forward and very DSL-like:
```kotlin
PolyglotConfig.configure {
lang = "Kotlin"
supported(true)
}
```
which is implemented as:
```kotlin
fun configure(config: PolyglotConfig.() -> Unit): PolyglotConfig {
val cfg = PolyglotConfig()
cfg.config()
return cfg
}
```
## Java
The Java configuration is provided using a `Consumer` which can be inlined for a more DSL-like feel.
```java
PolyglotConfig.configure(config -> {
config.lang("Java");
config.supported(true);
})
```
which is implemented as:
```kotlin
@JvmStatic
fun configure(consumer: Consumer): PolyglotConfig {
val config = PolyglotConfig()
consumer.accept(config)
return config
}
```
## Groovy
The Groovy configuration is exposed as a very Groovy DSL.
```groovy
PolyglotConfig.configure {
lang 'Groovy'
supported true
}
```
which is implemented as:
```kotlin
@JvmStatic
fun configure(@DelegatesTo(PolyglotConfig::class) closure: Closure): PolyglotConfig {
val config = PolyglotConfig()
closure.delegate = config
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()
return config
}
```
Note that this is basically the same code that would be called when doing this in Groovy itself.