V: Add support for expression blocks

Created on 3 Nov 2020  路  10Comments  路  Source: vlang/v

Hello! Me again 馃槅 .

I wanted to introduce you to this idea, as I find it somewhat useful in certain cases, and it is the support for expression blocks.

module main

import os

fn print_name(name string) {
    println("Your name is ${name}")
}

fn main() {
    print_name({
        mut name := os.input("Name: ")
        if name in ["root", "~"]  {
            eprintln("${name} not supported")
        }
        return name
    })
}

In C it would be:

string tmp_blockexpr_0;
{
    string name = os__input(_SLIT("Name: "));
    if (name == _SLIT("root") || name == _SLIT("~")) {
         eprintln(_SLIT(string__add(name, _SLIT(" not supported"))));
    }
    tmp_blockexpr_0 = name;
}
print_name(tmp_blockexpr_0);

How about the idea?

Feature Request

Most helpful comment

In V we already have implicit results for if, match and or expressions. For consistency we shouldn't use a keyword for an expression block.

I think Alex said a while ago he does want to support assigning a variable from a block. I'm not sure blocks should be allowed as a general expression, because naming things helps people to understand the code quicker. (Also it's tricky to untangle complex expressions and work out which needs evaluating first for cgen).

All 10 comments

you mean:

fn main() {
    print_name({
        mut name := os.input("Name: ")
        if name in ["root", "~"]  {
            eprintln("${name} not supported")
        }
        name
    })
}

without the return at the end.

You understood me very well. I put the return to understand that that variable is returned.

if alex wants that, it's definitely not prio 1

You could also do this:

import os

fn main()  {
    vartmp := {
        empty := os.input("array empty (true/false): ").bool()
        if empty {
              // the first return that is made defines the type of the variable
              return []int{}
        }
        return [1, 2, 3]
    }
    println(vartmp)
}

this will avoid making some variables mutable to change their value according to another value.

That's really unreadable

If we make them able to be re-evaluated at runtime we basically have Qt's property bindings which are very powerful

That's really unreadable

Really? How would you make it readable?

That's really unreadable

Really? How would you make it readable?

I would just write it as a function or write it separately.

module main

import os

fn print_name(name string) {
    println("Your name is ${name}")
}

fn main() {
    print_name({
        mut name := os.input("Name: ")
        if name in ["root", "~"]  {
            eprintln("${name} not supported")
        }
        return name
    })
}

I really find it hard to read at the first sight as it looks a function passed to a parameter because return is present plus it will also complicate and goes again one way of doing things. IMHO we shouldn't allow it.

Ok. How about using a keyword like assign?:

vartmp := {
   empty := os.input("array empty (true/false): ").bool()
   if empty {
      // the first `assign` that is made defines the type of the variable
      assign []int{}
   }
   assign [1, 2, 3]
}
module main

import os

fn print_name(name string) {
    println("Your name is ${name}")
}

fn main() {
    print_name(:= { //{
        mut name := os.input("Name: ")
        if name in ["root", "~"]  {
            eprintln("${name} not supported")
        }
        assign name
    })
}

In V we already have implicit results for if, match and or expressions. For consistency we shouldn't use a keyword for an expression block.

I think Alex said a while ago he does want to support assigning a variable from a block. I'm not sure blocks should be allowed as a general expression, because naming things helps people to understand the code quicker. (Also it's tricky to untangle complex expressions and work out which needs evaluating first for cgen).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

penguindark picture penguindark  路  3Comments

medvednikov picture medvednikov  路  3Comments

cjmxp picture cjmxp  路  3Comments

radare picture radare  路  3Comments

taojy123 picture taojy123  路  3Comments