scandum / tintin

TinTin++, aka tt++, is an extensible console MUD client.
https://tintin.mudhalla.net
GNU General Public License v3.0
201 stars 56 forks source link

Request a new syntax to declare multiple `#local` variables at once. #158

Closed dzpao closed 1 year ago

dzpao commented 1 year ago

Request a new syntax to declare multiple #local variables at once.

I like to keep my code's runtime space clean with the #local syntax. But if I have multiple #local variables to declare in a block, then I need to write many lines of #local, which makes the code look a bit bloated. So I'd like to be able to declare multiple #local at once.

scandum commented 1 year ago

You might be able to use #line local to make new variable declarations from #format and most other commands local.

That might reduce the number of declarations.

dzpao commented 1 year ago

Nice. WHY I FORGOT THIS.

But one problem with #line local in practice is that it introduces a layer of indentation that makes the whole block of code incoherent. Like this:

#alias foo {
    ........
    ........
    #line local {
        ..........
        ..........
    };
    ........
    ........
    #line local {
        ..........
        ..........
    };
    ........
    ........
};

Unless I wrap the whole block in #line local, like this:

#alias foo { #line local {
    ......
    ......
}};

Looks a bit strange. But I tried to give #line local to the front and it doesn't work properly again.

#line local #alias foo {
    .......
    .......
};

If only there was some way to make #alias/#action/#func all use local by default (unless declared with #var). This would effectively prevent different parts of the code block from reading the wrong values.

Or, simply, allowing #local to declare multiple variables at once might be a good way to go.

scandum commented 1 year ago
#alias foo { #line local {
    ......
    ......
}};

That would be the correct way to go about it since #line creates its own scope.

You could use nesting:

#local temp {};
#format temp[name] %-10s x;
#math temp[other_name] 1+1
dzpao commented 1 year ago

I understand that #alias foo { #line local { is good, but it looks a bit strange. Is it possible to make it a little more pretty?

scandum commented 1 year ago

I don't think so. There's also the foreach route:

#foreach {bli;bla;blo} {item} #local $item {}