DivKit Expression DSL

Core concepts

  • Expression — the base type for expressions producing a result of type T.
    • build(): String — builds the “inner” DivKit expression without the "@{…}" wrapper.
    • compile(): String — wraps the inner expression with "@{…}" for JSON serialization.
  • Expression types:
    • Literals of types integer, number, string, and boolean.
    • Variables Var<T>, created from a string name.
    • Arithmetic operations: unary minus -, addition +, subtraction -, multiplication *, division /, remainder %`.
    • Logical operations: negation !, logical and and, logical or or.
    • Comparison: equality equalTo, inequality notEqualTo, greater than moreThan, less than lessThan, greater than or equal to moreThanOrEqualTo, less than or equal to lessThanOrEqualTo.
    • String concatenation `+``.
    • Ternary operator ifElse.
    • Try operator tryExpression .
    • Function call functionName(args...).
      Parentheses and escaping:
    • Nested infix expressions, ternaries, and literal insertions are automatically parenthesized when necessary.
    • String literals are enclosed in single quotes; an apostrophe is escaped as '.
    • A number always has a decimal point (e.g., 1 → 1.0).
    • Negative numbers are printed as (-1), (-2.5).

Important: do not call compile() inside other builds — it will break apostrophe escaping.

Literals

Integers

  • val one: Expression<Long> = 1.integer()
  • val one: Expression<Long> = 1L.integer()

Floating-point numbers

  • val one: Expression<Double> = 1.number()
  • val one: Expression<Double> = 1L.number()
  • val one: Expression<Double> = 1.0f.number()
  • val one: Expression<Double> = 1.0.number()

Important: the decimal point is always preserved (e.g., 1 → 1.0).

Strings

  • val text: Expression<String> = "text".string()

Escaping: "O'Reilly".string() produces "'O\'Reilly'"

Boolean

  • val isTrue: Expression<String> = true.boolean()
  • val isFalse: Expression<String> = false.boolean()

Using a literal expression

  • val expr = "1 + 2".literalExpression<Long>()

Variables

Referencing a variable:

  • val exprVariable: Var<Boolean> = "variable_name".booleanVariable()
  • val exprVariable: Var<String> = "variable_name".stringVariable()
  • val exprVariable: Var<Double> = "variable_name".numberVariable()
  • val exprVariable: Var<Long> = "variable_name".integerVariable()
  • val exprVariable: Var<Url> = "variable_name".urlVariable()
  • val exprVariable>(): Var<Map<String, Any>> = "variable_name".dictVariable<Any>()
  • val exprVariable: Var<Long> = "variable_name".datetimeVariable()
  • val exprVariable>(): Var<List<Any>> = "variable_name".arrayVariable<Any>()
  • val exprVariable: Var<String> = "variable_name".colorVariable()

Arithmetic operations

Includes prefix unary minus, numeric operations and string concatenation.

Prefix unary minus

operator fun Expression<Long>.unaryMinus(): Expression<Long>

Example:

val count = "count".integerVariable()
val result = -count

operator fun Expression<Double>.unaryMinus(): Expression<Double>

Example:

val amount = "amount".numberVariable()
val result = -amount

Addition

operator fun Expression<Long>.plus(other: Expression<Long>): Expression<Long>

Overloads:

  • operator fun Expression<Long>.plus(other: Int): Expression<Long>
  • operator fun Expression<Long>.plus(other: Long): Expression<Long>
  • operator fun Int.plus(other: Expression<Long>): Expression<Long>
  • operator fun Long.plus(other: Expression<Long>): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result  = a + b

operator fun Expression<Double>.plus(other: Expression<Double>): Expression<Double>

Overloads:

  • operator fun Expression<Double>.plus(other: Double): Expression<Double>
  • operator fun Expression<Double>.plus(other: Float): Expression<Double>
  • operator fun Expression<Double>.plus(other: Int): Expression<Double>
  • operator fun Expression<Double>.plus(other: Long): Expression<Double>
  • operator fun Double.plus(other: Expression<Double>): Expression<Double>
  • operator fun Float.plus(other: Expression<Double>): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result  = a + b

Subtraction

operator fun Expression<Long>.minus(other: Expression<Long>): Expression<Long>

Overloads:

  • operator fun Expression<Long>.minus(other: Int): Expression<Long>
  • operator fun Expression<Long>.minus(other: Long): Expression<Long>
  • operator fun Int.minus(other: Expression<Long>): Expression<Long>
  • operator fun Long.minus(other: Expression<Long>): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result  = a - b

operator fun Expression<Double>.minus(other: Expression<Double>): Expression<Double>

Overloads:

  • operator fun Expression<Double>.minus(other: Double): Expression<Double>
  • operator fun Expression<Double>.minus(other: Float): Expression<Double>
  • operator fun Expression<Double>.minus(other: Int): Expression<Double>
  • operator fun Expression<Double>.minus(other: Long): Expression<Double>
  • operator fun Double.minus(other: Expression<Double>): Expression<Double>
  • operator fun Float.minus(other: Expression<Double>): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result  = a - b

Multiplication

operator fun Expression<Long>.times(other: Expression<Long>): Expression<Long>

Overloads:

  • operator fun Expression<Long>.times(other: Int): Expression<Long>
  • operator fun Expression<Long>.times(other: Long): Expression<Long>
  • operator fun Int.times(other: Expression<Long>): Expression<Long>
  • operator fun Long.times(other: Expression<Long>): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result  = a * b

operator fun Expression<Double>.times(other: Expression<Double>): Expression<Double>

Overloads:

  • operator fun Expression<Double>.times(other: Double): Expression<Double>
  • operator fun Expression<Double>.times(other: Float): Expression<Double>
  • operator fun Expression<Double>.times(other: Int): Expression<Double>
  • operator fun Expression<Double>.times(other: Long): Expression<Double>
  • operator fun Double.times(other: Expression<Double>): Expression<Double>
  • operator fun Float.times(other: Expression<Double>): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result  = a * b

Division

operator fun Expression<Long>.div(other: Expression<Long>): Expression<Long>

Overloads:

  • operator fun Expression<Long>.div(other: Int): Expression<Long>
  • operator fun Expression<Long>.div(other: Long): Expression<Long>
  • operator fun Int.div(other: Expression<Long>): Expression<Long>
  • operator fun Long.div(other: Expression<Long>): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result  = a / b

operator fun Expression<Double>.div(other: Expression<Double>): Expression<Double>

Overloads:

  • operator fun Expression<Double>.div(other: Double): Expression<Double>
  • operator fun Expression<Double>.div(other: Float): Expression<Double>
  • operator fun Expression<Double>.div(other: Int): Expression<Double>
  • operator fun Expression<Double>.div(other: Long): Expression<Double>
  • operator fun Double.div(other: Expression<Double>): Expression<Double>
  • operator fun Float.div(other: Expression<Double>): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result  = a / b

Remainder

operator fun Expression<Long>.rem(other: Expression<Long>): Expression<Long>

Overloads:

  • operator fun Expression<Long>.rem(other: Int): Expression<Long>
  • operator fun Expression<Long>.rem(other: Long): Expression<Long>
  • operator fun Int.rem(other: Expression<Long>): Expression<Long>
  • operator fun Long.rem(other: Expression<Long>): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result  = a % b

operator fun Expression<Double>.rem(other: Expression<Double>): Expression<Double>

Overloads:

  • operator fun Expression<Double>.rem(other: Double): Expression<Double>
  • operator fun Expression<Double>.rem(other: Float): Expression<Double>
  • operator fun Expression<Double>.rem(other: Int): Expression<Double>
  • operator fun Expression<Double>.rem(other: Long): Expression<Double>
  • operator fun Double.rem(other: Expression<Double>): Expression<Double>
  • operator fun Float.rem(other: Expression<Double>): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result  = a % b

String concatenation

operator fun Expression<String>.plus(other: Expression<String>): Expression<String>

Example:

val hello = "Hello, ".string()
val username = "username".stringVariable()

val result = hello + username

Logical operations

Prefix negation

operator fun Expression<Boolean>.not(): Expression<Boolean>

Example:

val flag = "flag".booleanVariable()
val result = !flag

Logical AND

infix fun Expression<Boolean>.and(other: Expression<Boolean>): Expression<Boolean>

Overloads:

  • infix fun Expression<Boolean>.and(other: Boolean): Expression<Boolean>
  • infix fun Boolean.and(other: Expression<Boolean>): Expression<Boolean>

Example:

val a = "a".booleanVariable()
val b = "b".booleanVariable()

val result = a and b

Logical OR

infix fun Expression<Boolean>.or(other: Expression<Boolean>): Expression<Boolean>

Overloads:

  • infix fun Expression<Boolean>.or(other: Boolean): Expression<Boolean>
  • infix fun Boolean.or(other: Expression<Boolean>): Expression<Boolean>

Example:

val a = "a".booleanVariable()
val b = "b".booleanVariable()

val result = a or b

Comparison operations

Equality

infix fun Expression<Long>.equalTo(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.equalTo(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.equalTo(other: Long): Expression<Boolean>
  • infix fun Int.equalTo(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.equalTo(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a equalTo b

infix fun Expression<Double>.equalTo(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.equalTo(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.equalTo(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.equalTo(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.equalTo(other: Long): Expression<Boolean>
  • infix fun Double.equalTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.equalTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.equalTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.equalTo(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a equalTo b

infix fun Expression<String>.equalTo(other: Expression<String>): Expression<Boolean>

Overloads:

  • infix fun Expression<String>.equalTo(other: String): Expression<Boolean>
  • infix fun String.equalTo(other: Expression<String>): Expression<Boolean>

Example:

val a = "a".stringVariable()
val b = "b".stringVariable()

val result = a equalTo b

infix fun Expression<Boolean>.equalTo(other: Expression<Boolean>): Expression<Boolean>

Overloads:

  • infix fun Expression<Boolean>.equalTo(other: Boolean): Expression<Boolean>
  • infix fun Boolean.equalTo(other: Expression<Boolean>): Expression<Boolean>

Example:

val a = "a".booleanVariable()
val b = "b".booleanVariable()

val result = a equalTo b

Inequality

infix fun Expression<Long>.notEqualTo(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.notEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.notEqualTo(other: Long): Expression<Boolean>
  • infix fun Int.notEqualTo(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.notEqualTo(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a notEqualTo b

infix fun Expression<Double>.notEqualTo(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.notEqualTo(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.notEqualTo(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.notEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.notEqualTo(other: Long): Expression<Boolean>
  • infix fun Double.notEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.notEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.notEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.notEqualTo(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a notEqualTo b

infix fun Expression<String>.notEqualTo(other: Expression<String>): Expression<Boolean>

Overloads:

  • infix fun Expression<String>.notEqualTo(other: String): Expression<Boolean>
  • infix fun String.notEqualTo(other: Expression<String>): Expression<Boolean>

Example:

val a = "a".stringVariable()
val b = "b".stringVariable()

val result = a notEqualTo b

infix fun Expression<Boolean>.notEqualTo(other: Expression<Boolean>): Expression<Boolean>

Overloads:

  • infix fun Expression<Boolean>.notEqualTo(other: Boolean): Expression<Boolean>
  • infix fun Boolean.notEqualTo(other: Expression<Boolean>): Expression<Boolean>

Example:

val a = "a".booleanVariable()
val b = "b".booleanVariable()

val result = a notEqualTo b

More than

infix fun Expression<Long>.moreThan(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.moreThan(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.moreThan(other: Long): Expression<Boolean>
  • infix fun Int.moreThan(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.moreThan(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a moreThan b

infix fun Expression<Double>.moreThan(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.moreThan(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.moreThan(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.moreThan(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.moreThan(other: Long): Expression<Boolean>
  • infix fun Double.moreThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.moreThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.moreThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.moreThan(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a moreThan b

More than or equal to

infix fun Expression<Long>.moreThanOrEqualTo(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.moreThanOrEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.moreThanOrEqualTo(other: Long): Expression<Boolean>
  • infix fun Int.moreThanOrEqualTo(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.moreThanOrEqualTo(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a moreThanOrEqualTo b

infix fun Expression<Double>.moreThanOrEqualTo(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.moreThanOrEqualTo(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.moreThanOrEqualTo(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.moreThanOrEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.moreThanOrEqualTo(other: Long): Expression<Boolean>
  • infix fun Double.moreThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.moreThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.moreThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.moreThanOrEqualTo(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a moreThanOrEqualTo b

Less than

infix fun Expression<Long>.lessThan(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.lessThan(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.lessThan(other: Long): Expression<Boolean>
  • infix fun Int.lessThan(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.lessThan(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a lessThan b

infix fun Expression<Double>.lessThan(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.lessThan(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.lessThan(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.lessThan(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.lessThan(other: Long): Expression<Boolean>
  • infix fun Double.lessThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.lessThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.lessThan(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.lessThan(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a lessThan b

Less than or equal to

infix fun Expression<Long>.lessThanOrEqualTo(other: Expression<Long>): Expression<Boolean>

Overloads:

  • infix fun Expression<Long>.lessThanOrEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Long>.lessThanOrEqualTo(other: Long): Expression<Boolean>
  • infix fun Int.lessThanOrEqualTo(other: Expression<Long>): Expression<Boolean>
  • infix fun Long.lessThanOrEqualTo(other: Expression<Long>): Expression<Boolean>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()

val result = a lessThanOrEqualTo b

infix fun Expression<Double>.lessThanOrEqualTo(other: Expression<Double>): Expression<Boolean>

Overloads:

  • infix fun Expression<Double>.lessThanOrEqualTo(other: Double): Expression<Boolean>
  • infix fun Expression<Double>.lessThanOrEqualTo(other: Float): Expression<Boolean>
  • infix fun Expression<Double>.lessThanOrEqualTo(other: Int): Expression<Boolean>
  • infix fun Expression<Double>.lessThanOrEqualTo(other: Long): Expression<Boolean>
  • infix fun Double.lessThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Float.lessThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Int.lessThanOrEqualTo(other: Expression<Double>): Expression<Boolean>
  • infix fun Long.lessThanOrEqualTo(other: Expression<Double>): Expression<Boolean>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()

val result = a lessThanOrEqualTo b

Conditional operations

Ternary operator

fun Expression<Boolean>.ifElse(onMatch: Expression<Long>, onMismatch: Expression<Long>): Expression<Long>

Overloads:

  • fun Expression<Boolean>.ifElse(onMatch: Expression<Long>, onMismatch: Int): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Expression<Long>, onMismatch: Long): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Int, onMismatch: Expression<Long>): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Long, onMismatch: Expression<Long>): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Int, onMismatch: Int): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Long, onMismatch: Long): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Int, onMismatch: Long): Expression<Long>
  • fun Expression<Boolean>.ifElse(onMatch: Long, onMismatch: Int): Expression<Long>

Example:

val a = "a".integerVariable()
val b = "b".integerVariable()
val condition = "condition".booleanVariable()

val result = condition.ifElse(a + 20, b - a)

fun Expression<Boolean>.ifElse(onMatch: Expression<Double>, onMismatch: Expression<Double>): Expression<Double>

Overloads:

  • fun Expression<Boolean>.ifElse(onMatch: Expression<Double>, onMismatch: Float): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Expression<Double>, onMismatch: Double): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Float, onMismatch: Expression<Double>): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Double, onMismatch: Expression<Double>): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Float, onMismatch: Int): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Float, onMismatch: Long): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Float, onMismatch: Float): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Float, onMismatch: Double): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Double, onMismatch: Int): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Double, onMismatch: Long): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Double, onMismatch: Float): Expression<Double>
  • fun Expression<Boolean>.ifElse(onMatch: Double, onMismatch: Double): Expression<Double>

Example:

val a = "a".numberVariable()
val b = "b".numberVariable()
val condition = "condition".booleanVariable()

val result = condition.ifElse(a + 20, b - a)

fun Expression<Boolean>.ifElse(onMatch: Expression<String>, onMismatch: Expression<String>): Expression<String>

Overloads:

  • fun Expression<Boolean>.ifElse(onMatch: String, onMismatch: Expression<String>): Expression<String>
  • fun Expression<Boolean>.ifElse(onMatch: Expression<String>, onMismatch: String): Expression<String>
  • fun Expression<Boolean>.ifElse(onMatch: String, onMismatch: String): Expression<String>

Example:

val a = "a".booleanVariable()
val b = "b".booleanVariable()
val condition = "condition".booleanVariable()

val result = condition.ifElse(a and b, a or b)

fun Expression<Boolean>.ifElse(onMatch: Expression<Boolean>, onMismatch: Expression<Boolean>): Expression<Boolean>

Overloads:

  • fun Expression<Boolean>.ifElse(
      onMatch: Boolean,
      onMismatch: Expression<Boolean>
    ): Expression<Boolean>
    
  • fun Expression<Boolean>.ifElse(
      onMatch: Expression<Boolean>,
      onMismatch: Boolean
    ): Expression<Boolean>
    
  • fun Expression<Boolean>.ifElse(onMatch: Boolean, onMismatch: Boolean): Expression<Boolean>

Example:

val success_message = "success_message".stringVariable()
val error_message = "error_message".stringVariable()
val condition = "condition".booleanVariable()

val result = condition.ifElse(success_message, error_message)

Try operator

Returns the left operand’s value; if its evaluation fails, returns the right operand’s value.

infix fun Expression<Long>.tryExpression(Expression<Long>): Expression<Long>

Overloads:

  • infix fun Expression<Long>.tryExpression(Int): Expression<Long>
  • infix fun Expression<Long>.tryExpression(Long): Expression<Long>
  • infix fun Int.tryExpression(Expression<Long>): Expression<Long>
  • infix fun Long.tryExpression(Expression<Long>): Expression<Long>
  • infix fun Int.tryExpression(Int): Expression<Long>
  • infix fun Long.tryExpression(Long): Expression<Long>

Example:

val missingInt = "missing_int".integerVariable()
val result = missingInt tryExpression 0

infix fun Expression<Double>.tryExpression(Expression<Double>): Expression<Double>

Overloads:

  • infix fun Expression<Double>.tryExpression(Double): Expression<Double>
  • infix fun Expression<Double>.tryExpression(Float): Expression<Double>
  • infix fun Double.tryExpression(Expression<Double>): Expression<Double>
  • infix fun Float.tryExpression(Expression<Double>): Expression<Double>
  • infix fun Float.tryExpression(Float): Expression<Double>
  • infix fun Double.tryExpression(Double): Expression<Double>

Example:

val missingNumber = "missing_number".numberVariable()
val result = missingNumber tryExpression 0.0

infix fun Expression<String>.tryExpression(Expression<String>): Expression<String>

Overloads:

  • infix fun Expression<String>.tryExpression(String): Expression<String>
  • infix fun String.tryExpression(Expression<String>): Expression<String>
  • infix fun String.tryExpression(String): Expression<String>

Example:

val missingString = "missing_string".stringVariable()
val result = missingString tryExpression "<missing>"

infix fun Expression<Boolean>.tryExpression(Expression<Boolean>): Expression<Boolean>

Overloads:

  • infix fun Expression<Boolean>.tryExpression(Boolean): Expression<Boolean>
  • infix fun Boolean.tryExpression(Expression<Boolean>): Expression<Boolean>
  • infix fun Boolean.tryExpression(Boolean): Expression<Boolean>

Example:

val missingBoolean = "missing_boolean".booleanVariable()
val result = missingBoolean tryExpression false

Function calls

Built-in DivKit functions are documented on a separate page..

function<T>(functionName: String, vararg args: Expression<*>): Expression<T>

Allows you to use an arbitrary function, including one declared in the layout.

Samples

Accessing a variable

val username = "username".stringVariable()

val card = divan {
    data(
        logId = "variable_test",
        variables = listOf(
            divkitVariable(
                variable = username,
                value = "User"
            )
        ),
        div = text(
            fontSize = 16
        ).evaluate(
            text = ("Hello, ".string() + username).divanExpression()
        )
    )
}

Serialization result:

{
  "card": {
    "log_id": "variable_test",
    "states": [
      {
        "state_id": 0,
        "div": {
          "type": "text",
          "text": "@{'Hello, ' + greeting}",
          "font_size": 16
        }
      }
    ],
    "variables": [
      {
        "type": "string",
        "name": "username",
        "value": "User"
      }
    ]
  },
  "templates": {}
}

Theme support

const val THEME_LIGHT = "light"
const val THEME_DARK = "dark"

val theme = "theme".stringVariable()

val card = divan {
    data(
        logId = "theme_test",
        div = text(
            text = "Lorem ipsum",
            fontSize = 16
        ).evaluate(
            textColor = (theme equalTo THEME_LIGHT).ifElse(
                onMatch = "#000000".string(),
                onMismatch = "#FFFFFF".string()
            ).divanExpression()
        )
    )
}

Serialization result:

{
  "card": {
    "log_id": "theme_test",
    "states": [
      {
        "state_id": 0,
        "div": {
          "type": "text",
          "text": "Lorem ipsum",
          "font_size": 16,
          "text_color": "@{(theme == 'light') ? '#000000' : '#FFFFFF'}"
        }
      }
    ]
  },
  "templates": {}
}

Calling a function

val priceFormat = "#,###.##".string()
val locale = "ru-RU".string()
val price = "price".integerVariable()

val card = divan {
    data(
        logId = "function_call_test",
        variables = listOf(
            divkitVariable(
                variable = price,
                value = 1000
            )
        ),
        div = text(
            fontSize = 16
        ).evaluate(
            text = (decimalFormat(price, priceFormat, locale) + " ₽".string()).divanExpression()
        )
    )
}

Serialization result:

{
  "card": {
    "log_id": "function_call_test",
    "states": [
      {
        "state_id": 0,
        "div": {
          "type": "text",
          "text": "@{decimalFormat(price, '#,###.##', 'ru-RU') + ' ₽'}",
          "font_size": 16
        }
      }
    ],
    "variables": [
      {
        "type": "integer",
        "name": "price",
        "value": 1000
      }
    ]
  },
  "templates": {}
}

Updating variable values

val complexData = "complex_data".dictVariable<Any>()

val card = divan {
  data(
          logId = "variable_change_test",
          variables = listOf(
                  divkitVariable(
                          variable = complexData,
                          value = mapOf(
                                  "integer_param" to 10,
                                  "number_param" to 10.0,
                                  "string_param" to "Lorem ipsum",
                                  "boolean_param" to false
                          )
                  )
          ),
          div = text(
                  text = "Update data",
                  fontSize = 16,
                  actions = listOf(
                          action(
                                  logId = "update_integer_param",
                                  typed = actionDictSetValue(
                                          variableName = complexData.name,
                                          key = "integer_param",
                                          value = integerValue(value = 20)
                                  )
                          ),
                          action(
                                  logId = "update_number_param",
                                  typed = actionDictSetValue(
                                          variableName = complexData.name,
                                          key = "number_param",
                                          value = numberValue(value = 20.0)
                                  )
                          ),
                          action(
                                  logId = "update_string_param",
                                  typed = actionDictSetValue(
                                          variableName = complexData.name,
                                          key = "string_param",
                                          value = stringValue(value = "Dolor sit amet")
                                  )
                          ),
                          action(
                                  logId = "update_boolean_param",
                                  typed = actionDictSetValue(
                                          variableName = complexData.name,
                                          key = "boolean_param",
                                          value = booleanValue(value = true)
                                  )
                          )
                  )
          )
  )
}

Serialization result:

{
  "card": {
    "log_id": "variable_change_test",
    "states": [
      {
        "state_id": 0,
        "div": {
          "type": "text",
          "text": "Update data",
          "actions": [
            {
              "log_id": "update_integer_param",
              "typed": {
                "type": "dict_set_value",
                "key": "integer_param",
                "value": {
                  "type": "integer",
                  "value": 20
                },
                "variable_name": "complex_data"
              }
            },
            {
              "log_id": "update_number_param",
              "typed": {
                "type": "dict_set_value",
                "key": "number_param",
                "value": {
                  "type": "number",
                  "value": 20.0
                },
                "variable_name": "complex_data"
              }
            },
            {
              "log_id": "update_string_param",
              "typed": {
                "type": "dict_set_value",
                "key": "string_param",
                "value": {
                  "type": "string",
                  "value": "Dolor sit amet"
                },
                "variable_name": "complex_data"
              }
            },
            {
              "log_id": "update_boolean_param",
              "typed": {
                "type": "dict_set_value",
                "key": "boolean_param",
                "value": {
                  "type": "boolean",
                  "value": 1
                },
                "variable_name": "complex_data"
              }
            }
          ],
          "font_size": 16
        }
      }
    ],
    "variables": [
      {
        "type": "dict",
        "name": "complex_data",
        "value": {
          "integer_param": 10,
          "number_param": 10.0,
          "string_param": "Lorem ipsum",
          "boolean_param": false
        }
      }
    ]
  },
  "templates": {}
}

Learn more

You can discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_en.

DivKit Repository