Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global Scope API (unsafe) #811

Open
emil14 opened this issue Dec 18, 2024 · 0 comments
Open

Global Scope API (unsafe) #811

emil14 opened this issue Dec 18, 2024 · 0 comments
Assignees
Labels

Comments

@emil14
Copy link
Collaborator

emil14 commented Dec 18, 2024

The idea is implement API that will allow components to share data without exlicit message passing

Obviously, this should't be preferred style for most cases, but it might be a useful workaround.

import {
    fmt
    runtime
    runtime/global
}

def SetNameToJohn(sig any) (sig any, err error) {
    set global.Set?
    ---
    :sig -> 'name' -> set:k
    'john' -> set:v
    set -> :sig
}

def PrintName(sig any) (sig any) {
    get global.Get
    println fmt.Println
    ---
    :sig -> 'name' -> get -> println -> :sig
}

def Main(start any) (start any) {
    set_name SetNameToJohn
    print_name PrintName
    panic runtime.Panic
    ---
    :start -> set_name -> print_name
    print_name:res -> :stop
    print_name:err -> panic
}

global package implements Key-Value storage interface that might be visualized this way

pub interface IKeyValueStorage<K, V> {
    Set(k K, v V) (any)
    Get(K) (res V, err error)
}

Global scope must be used with caution, because dataflow is not explicit. As soon as we use global scope, we must guarantee dataflow ourself, without relying on compiler.

For example, if we swap order of set_name and print_name in Main component, we will get runtime error.

def Main(start any) (start any) {
    set_name SetNameToJohn
    print_name PrintName
    ---
    :start -> print_name
    print_name:res -> set_name -> :stop
    print_name:err -> panic
}

This example however is too simple to express the danger of this style. In other words, without it, compiler can guarantee that message X is available, when we reffer to it. This API changes this.

To visualize it better we need to imagine a deep hierarhy of components where betwen parent that sets or reads the value, and other side of this "channel" is a tree of several levels. Components can talk this way in any structure, any order.


UPD this proposal has same problem as context API from #812

Any message we get from global scope is any and we need to be able to assert a specific type at runtime. More than that, it must play nice with pattern matching

@emil14 emil14 added the Idea label Dec 18, 2024
@emil14 emil14 self-assigned this Dec 18, 2024
This was referenced Dec 18, 2024
@emil14 emil14 changed the title Global Scope API Global Scope API (unsafe) Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant