# A syntax-level support for reactive ref type

**URL:** <https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788>\
**Category:** Language Design\
**Created:** [October 12, 2025, 6:25pm UTC](https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788 "2025-10-12T18:25:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhengkeli](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/zhengkeli/32/3974_2.png) [@zhengkeli](https://discuss.kotlinlang.org/u/zhengkeli)\
**Post date:** [October 12, 2025, 6:25pm UTC](https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788/1 "2025-10-12T18:25:30Z")

</div>

A reactive pipeline is very commonly used in various sicarios. In frontend development, we use such pipelines to bind the states and views in many ways. For example, we have `ref` and `computed` in Vue. We have re-executing mechanism in React and Compose. In server side development, we use streams and flows to process sequential data.

However, to define the reactive pipeline is not that intuitive. For example, to define `c=a+b`, we have to write a lambda with explicit specifications. The lambda thing prevent us to write complicated pipelines conveniently.

```kotlin
val a_flow:Flow<Int> = flowOf(1, 2)
val b_flow:Flow<Int> = flowOf(10, 20)
val c_flow:Flow<Int> = a_flow.zip(b_flow) { a, b -> a + b }

```

I believe that it can be much better if we provide a syntax-level support. To be short, the syntax allows us to write reacive pipelines intuitively (just `a+b`) as following.

```kotlin
var a_ref: var ref<Int> = 1
var b_ref: var ref<Int> = 10
val c_ref: val ref<Int> = a + b

unref(c_ref) // 11

a_ref = 2
b_ref = 20
unref(c_ref) // 22

```

Here I introduced **ref types** which are something like pointers in C, references in C++ or `Ref` in Vue. The code can be compiled in desugaring stages into the following.

```kotlin
val a_ref: VarRef<Int> = VariableRef(1)
val b_ref: VarRef<Int> = VariableRef(10)
val c_ref: ValRef<Int> = ComputedRef(a_ref, b_ref) { a, b -> a + b }

c_ref.get() // 11

a_ref.set(2)
b_ref.set(20)
c_ref.get() // 22

```

The compiler automatically converts all operations applied on ref values. Specifically, here “all operations” means being used as an argument in any functions. In this cases function `add(a: Int, b:Int)`.

Functions can also mark its arguments or return value as a ref, which allows it to accept the reference itself but not the referred value.

```auto
fun addPipeline(a_ref: val ref<Int>, b_ref: val ref<Int>): val ref<Int> {
    return a+b;
}

```

When calling a function, a reference (`ref<Int>`) and be cast into a value (`Int`) making it returns a computed reference. (It can be nested)

When assigning to variable or argument, a value (`Int`) can be cast into a constant reference (`val ref<Int>`) for convenience; a mutable reference (`var ref<Int>`) can be cast into an immutable reference (`val ref<Int>`).

A series of special functions are intorduced to bridge the references and desugared objects. So that we can easliy operate a reference as an object, vise versa.

```auto
fun wrap(a: val ref<Int>): ValRef<Int>

fun wrap(a: var ref<Int>): VarRef<Int>

fun unwrap(a: ValRef<Int>): val ref<Int>

fun unwrap(a: VarRef<Int>): var ref<Int>

fun <T> unref(a: val ref<T>): T = wrap(a).get();

```

We can define custom subclass of `ValRef` and `VarRef` to define how the values are computed or assigned.

Futher more, we can introduce the `suspend val ref<Int>` to allow waiting/watching the value changes asynchronously.

And it make it possible to perform optimizations at the compiler level (like fusing several operations into one lambda).

What do you think about this idea?

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [October 13, 2025, 2:37am UTC](https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788/2 "2025-10-13T02:37:06Z")

</div>

Looks like atomics and operator functions? I’m not really understanding what you’re proposing here, or how it helps. Can you provide some concrete examples, of something you would do in the front end using Vue, and then how you would like to do the same thing in Kotlin, and why you can’t do it currently?

You can define an operator plus function to add two Flows together, to prevent having to call `zip`.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [October 15, 2025, 2:44am UTC](https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788/3 "2025-10-15T02:44:34Z")

</div>

> [@Skater901](#):
>
> You can define an operator plus function to add two Flows together, to prevent having to call `zip`.

There is already a plus operator function that appends one flow to another.

But the original example can be shortened using Int:plus:

```auto
    val c_flow: Flow<Int> = a_flow.zip(b_flow, Int::plus)

```

I too do not understand what exactly was being proposed and how it helps

It looks like his VarRef and ComputedRef can all be shortened to:

```auto
    var a_ref = 1
    var b_ref = 10
    val c_ref: Int get() = a_ref + b_ref
    c_ref // 11

    a_ref = 2
    b_ref = 20
    c_ref // 22

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [October 15, 2025, 9:40pm UTC](https://discuss.kotlinlang.org/t/a-syntax-level-support-for-reactive-ref-type/30788/4 "2025-10-15T21:40:58Z")

</div>

I think what you’re looking for are monad comprehensions, or in general, direct-style effects.  
I have a [library](https://github.com/kyay10/KonTinuity/tree/main) that enables such a use-case. Your example would thus look like:

```kotlin
val aFlow = flowOf(1, 2)
val bFlow = flowOf(10, 20)
val cFlow = runFlowCC { aFlow.bind() + bFlow.bind() }

```

[Molecule](https://github.com/cashapp/molecule) also does this, but only for `StateFlow`s, using some Compose magic. It’s very useful for `StateFlow`s, but doesn’t have the full generality of my library.  
Effect interaction is especially of great importance here. The full generality of effects allows for incredibly intricate programs to be written succinctly. I have truly marvelous examples of this, which this forum post is too narrow to contain…

In general though, your idea seems to boil down to `StateFlow`s, and for that, Compose and Molecule do an incredible job already, so I’d really suggest you look at those.
