Alexey Novik
12/07/2020, 1:52 PMShawn Tucker
02/09/2021, 8:50 PMVenkat
03/12/2021, 11:33 AM"""
html {
head {
title {+"XML encoding with Kotlin"}
}
body {
h1 {+"XML encoding with Kotlin"}
p {+"this format can be used as an alternative markup to XML"}
}
}
"""
is there any easy way to convert this to Kotlin DSL object ? or it needs to parsed line by line?Stefan Beyer
11/19/2021, 1:59 PMA
expands to Any
and everything is fine...
--------------
Original question:
I just fiddled around with some DSL stuff and found some weird behavior.
Consider these two files. One is inside a library, defining some DSL:
package some.library.dsl
class Builder {
fun <A> foo(first: A, second: A) { println("MEMBER called with $first and $second") }
}
fun build(config: Builder.()->Unit) = Builder().apply(config)
And one is inside a consuming app:
package <http://my.app|my.app>
import some.library.dsl.Builder
import some.library.dsl.build
fun <A, B> Builder.foo(first: A, second: B) { println("EXTENSION called with $first and $second") }
fun main() {
build {
foo(1, 2)
foo("hello", 0.4) // ???
}
}
I expected a compile error at the line marked with // ???
, but the code compiles, runs and prints the following:
MEMBER called with 1 and 2
MEMBER called with hello and 0.4Is this expected behavior? As I see it, this could break existing dsl libraries, as they could receive arguments of unexpected types, right?
vladimirsitnikov
11/24/2021, 7:36 AM@DslMarker
means.
Can anyone please clarify what @DslMarker
annotation on a function means?
The official documentation (see https://kotlinlang.org/docs/type-safe-builders.html#scope-control-dslmarker, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-dsl-marker/) describes that the annotations affect classes, interfaces, and lambdas only. The documentation provides no clue on the meaning of @DslMarker fun html(...) {
In practice, both kotlinx-html
and ktor
annotate their DSL functions with the corresponding DslMarker annotation: https://github.com/Kotlin/kotlinx.html/blob/24ef7f418687ce4241ec111f757b09e8b4f5bf79/src/commonMain/kotlin/generated/gen-tags-p.kt#L49-L50
Here's an example:
@HtmlTagMarker
inline fun PICTURE.img(alt : String? = null, src : String? = null, classes : String? = null, crossinline block : IMG.() -> Unit = {}) : Unit =
IMG(attributesMapOf("alt", alt,"src", src,"class", classes), consumer).visit(block)
Of course, their Tag
interface is annotated (PICTURE
and IMG
implement that interface):
@HtmlTagMarker
interface Tag {
However, my question is what does annotation on function mean?
As a side-effect, I noticed that IDEA 2021.2.3 removes italic style from extension functions that have @DslMarker
annotation. Is it a valid hack? Should it be documented somehow?karn
01/12/2022, 7:57 PMAdvitiay Anand
03/14/2022, 9:07 PMAlex Cruise
08/19/2022, 11:02 PMAlex Cruise
08/22/2022, 3:58 PMAlex Cruise
08/22/2022, 4:12 PMShawn
08/22/2022, 4:45 PMAlex Cruise
08/22/2022, 4:49 PMAlex Cruise
08/22/2022, 4:50 PMx not-followed-by y
🙂Alex Cruise
08/22/2022, 4:51 PMThe not-predicate expression !e succeeds if e fails and fails if e succeeds, again consuming no input in either case.
Alex Cruise
08/22/2022, 4:51 PMAlex Cruise
08/23/2022, 12:42 AMCurtis Ullerich
10/06/2022, 8:39 PMval matcher = plus { num { where { it.value > 2 } } variable {} }
val ast = Addition(Number(3), Variable("y"))
val matched = matcher.matches(ast)
I'm hoping to also support property extraction. I implemented something that works, but it feels gross using nullable vars like this:
var value: Int? = null
var name: String? = null
val extracter = plus { num { extract { value = it.value } } variable { extract { name = it.name } } }
val ast = Addition(Number(3), Variable("y"))
extracter.extractFrom(ast)
println("$value $name") // 3 y
Is there a pattern I could use to implement something more like this?
val (value: Int, name: String) = extracter.extractFrom(ast) ?: return null
Viktor Sirotin
01/28/2023, 4:18 PMViktor Sirotin
01/31/2023, 1:00 PMDirk Hoffmann
03/14/2023, 3:15 PM<mailto:this@dsl.xxx|this@dsl.xxx>
) and I don't understand WHY.
I constructed a single executable File to showcase my "problem" (at the end of the file/code)Bradleycorn
05/10/2023, 2:31 PMval appConfig = AppConfig {
name = "My App"
storage {
filePath = "/path/to/some/dir"
}
network {
baseUrl = "<https://api.mysite.com>"
authType = "Basic"
}
}
Wondering if someone might provide some advice on the best way to do this? I have written some code that works, but as this is my first attempt, I’m wondering if I’ve done it the “right”/“best” way. Code is in the 🧵Dirk Hoffmann
05/16/2023, 7:28 AMctxObjs access does NOT work in constructor:
class CtxClass(val value: Int)
data class CtxStore(val ctxObjs: MutableMap<String, CtxClass> = mutableMapOf())
context(CtxStore)
class SomeClass(var ctxO: CtxClass = ctxObjs["someRef"]!!) { // <-- Unresolved reference: ctxObjs
fun someFun() = "SomeClass(${ctxO.value})"
}
ctxObjs access does work in init block:
class CtxClass(val value: Int)
data class CtxStore(val ctxObjs: MutableMap<String, CtxClass> = mutableMapOf())
context(CtxStore)
class SomeClass(var ctxO: CtxClass = CtxClass(0)) {
init {
ctxO = ctxObjs["someRef"]!!
}
fun someFun() = "SomeClass(${ctxO.value})"
}
smallufo
07/09/2023, 9:18 AMMax
07/09/2023, 7:37 PMMax
08/04/2023, 7:16 AM윤동환
08/10/2023, 2:07 AMT.() -> R
and (T) -> R
for function type?
First one called lambda with receiver and second one is the first class function.neerav
08/31/2023, 8:48 AMandroid { signingConfig, defaultConfig etc }
dependencies {}
From above Case I am able to move dependencies { }
into separate dependencies.gradle.kts
file.
Now i am facing issue when I am trying to move signingConfig & defaultConfig etc into separate flavorSigning.gradle.kts
file
Can anyone please help me how can we move defaultConfig and productFlavor code into dedicated gradle.kts file ?
I am not sure where this question should be asked so please guide me if its in wrong channel.
Thank youwakingrufus
10/18/2023, 7:14 PMyolocat
01/19/2024, 4:57 PMMarco Pierucci
02/23/2024, 12:47 PMlibs
notation within convention plugins?
I've seen some using val libs = the<LibrariesForLibs>()
But as per https://github.com/gradle/gradle/issues/19813 thats not intended