nevalang / neva

🌊 Dataflow programming language with static types and implicit parallelism. Compiles to native code and Go
https://nevalang.org
MIT License
94 stars 7 forks source link

Type-safe string interpolation syntax sugar #762

Open emil14 opened 1 day ago

emil14 commented 1 day ago

Problem

Building a formatted string is kinda verbose. You'll probably end up by creating small formatter components per each use case. On the other hand in e.g. JavaScript string interpolation is super easy - ${a} + ${b} = ${a + b}.

import { fmt }

def XXX(a int, b int) (c string, err error) {
    fmt.Printf<int>?
    ---
    '$ + $ = $' -> printf:tpl
    a -> printf:args[0]
    b -> printf:args[1]
    (a + b) -> printf:args[2]
    printf -> :c
}

Proposal

Introduce new type of sender, that can be processed by both analyzer (ensure everything is correct) and desugarer (translate to "before" version)

def XXX(a int, b int) (c string) {
    `{:a} + {:b} = {(:a + :b)}` -> :c
}

Analyzer Rules

emil14 commented 1 day ago
type Format string {
    Template string
    Args []Sender
}