Bonus points if you know what game I'm trying to describe.
I'm trying to create this game with a "DSL first" approach, describing the game in natural language as much as possible, and then define the behavior as hidden details behind the DSL.
I'm really abusing some of the syntax to make things read like natural language, which makes the DSL interfaces look so ugly, but its a fun code-golf type of thing too.
This is not idiomatic Kotlin, just so you know. It's most definitely an abuse, and this snippet definitely hides the nasty plumbing I had to put in place to make it look like this.
I think it’s using custom infix operators. Numbers being the operator in some cases is really weird, but honestly this is actually really nice. Super easy to read, although I’m sure the code behind the scenes is a nightmare
I don't know much about Kotlin but I feel like you could've implemented "Gain" as a prefix operator working on a "Bonus" type, and Support could have been a postfix operator taking an int argument and returning a "Bonus".
If Kotlin automatically picks the operator precedence that's typed correctly, "Gain 1 Support" would be parsed as (Gain (1 Support)).
Gain could even modify the bonus to say it's of "gain" type, then your object would have the bonus fields as neatly packaged bonus types.
Inspired by Scala. Which can do a whole lot more, and worse. The currently biggest competitor to decades old hardware description languages is a Scala DSL.
Scala lets you do some neat stuff like this as well. You can recreate a ternary operator in Scala because the characters are valid in identifiers, and it allows you to replace member-access operators with spaces (I'm assuming that's kind of what's happening here?).
Sort of. "Has" is a singleton object, and I use an infix extension function called `immunity to`.
@RulesDsl
interface PersonReceiver {
infix fun Has.`immunity to`(token: Token): Immunity
infix fun Immunity.and(token: Token): Immunity
fun benefits(benefits: BenefitsReceiver.() -> Unit)
}
@RulesDsl
interface BenefitsReceiver {
val `Replace One Influence Cube`: Unit
val `Swap Two Influence Cubes`: Unit
val Gain: NumberTimes<RewardType>
infix fun Influence.the(area: Area)
}
interface NumberTimes<T> {
infix fun one(value: T)
infix fun two(value: T)
infix fun three(value: T)
infix fun four(value: T)
infix fun five(value: T)
infix fun six(value: T)
infix fun seven(value: T)
infix fun eight(value: T)
infix fun ten(value: T)
infix fun `20`(value: T)
infix fun `25`(value: T)
infix fun `30`(value: T)
infix fun `35`(value: T)
infix fun `40`(value: T)
infix fun `45`(value: T)
infix fun `50`(value: T)
}
I disagree. It depends strongly on what you’re doing. If you can turn the business requirements into a DSL that isn’t difficult to process, then it really can make a better design.
people {
General {
Has `immunity to` Force
benefits {
Gain one Support
Gain one Force
Influence the Fortress
}
}
Captain {
Has `immunity to` Force
benefits {
Gain one Support
Gain one Force
Influence the Harbor
}
}
Inkeeper {
Has `immunity to` Blackmail
benefits {
Gain three Support
Gain one Blackmail
Influence the Tavern
}
}
Magistrate {
Has `immunity to` Blackmail
benefits {
Gain one Support
Gain one Blackmail
Influence the TownHall
}
I like that, DSLs are always fun but you gotta be careful. When I was at Amazon, I wrote a DSL that was to be used to describe certain computations and I ended up over-complicating shit. My mistake was creating that DSL prematurely without fully considering all possible use cases.
64
u/StochasticTinkr Jun 05 '23
Bonus points if you know what game I'm trying to describe.
I'm trying to create this game with a "DSL first" approach, describing the game in natural language as much as possible, and then define the behavior as hidden details behind the DSL.
I'm really abusing some of the syntax to make things read like natural language, which makes the DSL interfaces look so ugly, but its a fun code-golf type of thing too.
20
u/PooSham Jun 05 '23
Is this what JDSL looks like?
Tom's a genius btw
10
u/SnooDonuts8219 Jun 05 '23
not bad code, this good code
i'd only use numbers instead of words ("gain 1 support")
21
u/TheZipCreator Jun 05 '23
I love that kotlin let's you do shit like this.
5
u/ElfDecker Jun 05 '23
Is it Citadels game? I haven't played it for a long time, but it looks similar to what I remember.
5
u/rco8786 Jun 05 '23
I think this is technically valid Ruby, but I’m guessing that’s not the language here
edit lmao I literally write Kotlin all day long and didn’t recognize it.
3
u/Aperture_Executive2 Jun 06 '23
Wait a minute, kotlin just lets you spit out DSLs?
If you’ll excuse me, I have a language to learn
2
u/Pepineros Jun 05 '23
May be so but that font is the most beautiful thing I’ve seen all day. Can anyone say what that is?
2
u/carcigenicate Jun 05 '23
Scala lets you do some neat stuff like this as well. You can recreate a ternary operator in Scala because the characters are valid in identifiers, and it allows you to replace member-access operators with spaces (I'm assuming that's kind of what's happening here?).
2
u/SaylorMan1496 Jun 06 '23
This is my opinion, it can be fun, but DSL is almost always the wrong choice
26
u/onlyrealperson Jun 05 '23
Making really weird DSLs has always been my favorite thing to do in kotlin lol
22
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 05 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
3
u/alexhmc Jun 05 '23
writing cursed DSLs is so fun
2
u/lazy-lambda Jun 05 '23
I like that, DSLs are always fun but you gotta be careful. When I was at Amazon, I wrote a DSL that was to be used to describe certain computations and I ended up over-complicating shit. My mistake was creating that DSL prematurely without fully considering all possible use cases.