Weird Idiom: Labeling Values?

I'm going through and changing a lot of FUNCTION uses to FUNC + LET. The long term goal will be to eliminate the locals-gathering functionality...and you either use LET/USE or you go explicit with <local> in the function spec.

I came across this:

    size-as-binary: enbin [be + 8] length of embedding
    append executable size-as-binary

This was the only use of the variable. Changing it to a LET works, but it's kind of a waste:

    let size-as-binary: enbin [be + 8] length of embedding
    append executable size-as-binary

LET's assignment form does run the right hand side...because the LET word itself vaporizes, so it's like the bound new SET-WORD! just runs as normal:

    append executable let size-as-binary: enbin [be + 8] length of embedding

But a LET isn't really necessary here. I had a weird thought, what if a TAG! or ISSUE! or something was used in a way that was "obviously" throwing it away?

    append executable (<size-as-binary> enbin [be + 8] length of embedding)

Maybe a little confusing. But it saves on a variable declaration.

A comment is zero overhead, so I'm doing that...

    append executable enbin [be + 8] length of embedding  ; size of binary

But I just thought the idea of a deliberately discarded leading value was kind of interesting.

1 Like

I'm curious to know what others think about this.
I prefer not to chain things in long, dense expressions. Makes it harder to read and debug later (or to add error-handling). Sometimes minimizing variable use is not a viable option.

It's worthy of some thought. In the example...

it seems like it would be additional complexity to distinguish/escape a <tag> or #issue that you want to keep in the APPEND expression.

2 Likes

Rust has a weird convention where names that start with underscore are placeholders. This is to help avoid warnings about unused variables.

fn some_function(foo: String, bar: u32) {
   println("bar is {}", bar);
   // you get a warning that `foo` is unused
}

fn another_function(_foo: String, bar: u32) {
   println("bar is {}", bar);
   // no warning, because foo starts with underscore
}

It doesn't actually stop you from referring to the variable--it's a legal name. It just won't warn you when you don't use it.

We don't have analysis that does warnings :-/ but could use the same convention _name...since you can use _ to opt out.

There's more possibilities of actually using a different type, to prevent the declaration of variables at all...

let name: "Eve"
for-each [#name score] ["Bob" 1020 Alice "304"] [
    ; don't actually create a variable for NAME, just a placeholder
    print [name]  ; this would thus print "Eve"
]

How truly useful that would be, I don't know.

1 Like