News for hackers. Stuff that matters.
Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @01:37AM (#38187421)
For me personally that's the part of syntax I don't like and don't understand the value of.
Re:Gleam: a type safe language on the Erlang VM by danappelxx on Wednesday November 08, 2023 @01:45AM (#38187460)
Generally stems from the philosophy that code is read more than written, and this helps readability.
Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @04:08AM (#38188187)
Does it? Instead of a single parameter you now have two names, and a type info to boot

    pub fn replace(
      in string: String,
      each pattern: String,
      with replacement: String,
    ) {
      // The variables `string`, `pattern`, and `replacement` are in scope here
    }

    replace(in: "A,B,C", each: ",", with: " ")
vs

    pub fn replace(
      in: String,
      each: String,
      with: String,
    ) {

    }

    replace(in: "A,B,C", each: ",", with: " ")
Re:Gleam: a type safe language on the Erlang VM by _glass on Wednesday November 08, 2023 @07:28AM (#38189512)
Wouldn't you have replace("A,B,C", ",", " "), which is quite unreadable.
Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @12:17PM (#38193608)
Re:Gleam: a type safe language on the Erlang VM by lpil on Wednesday November 08, 2023 @11:03AM (#38192324)
When argument variable names and labels are combined you have two problems:

1. Labels are accidental. If all arguments automatically create labels then the programmer has not considered and deliberately designed the API. Gleam is designed to make sure that APIs are always carefully thought about and designed as appropriate.

2. Renaming a variable becomes a breaking change. We don't believe that renaming a variable should ever result in a semver major version bump.

Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @12:13PM (#38193533)
> Labels are accidental. If all arguments automatically create labels then the programmer has not considered and deliberately designed the API.

1. Not every code is an API. And APIs exist in multiple languages that don't have labels

2. If you wanted named parameters, you could go the C# way: https://learn.microsoft.com/en-us/dotnet/csharp/programming-...

3. Since Gleam doesn't enforce labels or check labels in any way, it doesn't "make sure APIs are carefully thought out". Labels will be just as random and accidental as parameter names following the whims of the programmer who decides to use them

> Renaming a variable becomes a breaking change.

So does renaming a label.

Re:Gleam: a type safe language on the Erlang VM by yaglo on Friday November 10, 2023 @07:23AM (#38218005)
1. Every class/method/function/whatever declaration is an API, doesn't matter if it's internal or which language it's written. 2. Why do you think it's better? It looks like it tries to cater to everyone, and leads to inconsistent API usage and requires teams to agree on linting rules and use tools to enforce consistent coding standards. 3. It's a tool for _you_ to design good APIs that are clear. Languages without named arguments don't give you this tool.

> So does renaming a label.

In languages without this feature, any renaming breaks the API, unlike the ones with distinct internal and external names. This is not the same.

Re:Gleam: a type safe language on the Erlang VM by zem on Wednesday November 08, 2023 @01:01PM (#38194324)
the idea is that within the function body you would rather refer to the variables as e.g. `for s in string.match(pattern)` than `for s in in.match(each)`
Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @02:23PM (#38195576)
Then call them that.

`replace(string, pattern)` is just as (if not more) readable as `replace(in, each)`.

I was specifically replying to the claim about readability.

Re:Gleam: a type safe language on the Erlang VM by dylukes on Wednesday November 08, 2023 @02:09AM (#38187556)
It separates "names as public-facing API design decisions" from "names as developer conveniences for expressing intent and clarifying their code".

These are often very at odds. And if you don't want to repeat yourself twice with two identical names... you don't have to.

And unlike Smalltalk, in Swift at least, externally unnamed but internally named is easy (just make the external name _).

Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @04:09AM (#38188191)
> It separates "names as public-facing API design decisions" from "names as developer conveniences for expressing intent and clarifying their code"

How often is this an issue?

> And unlike Smalltalk, in Swift at least, externally unnamed but internally named is easy (just make the external name _).

So, visual clutter for very little gain. IMO

Re:Gleam: a type safe language on the Erlang VM by lpil on Wednesday November 08, 2023 @11:04AM (#38192332)
> How often is this an issue?

In our opinion, almost always.

Re:Gleam: a type safe language on the Erlang VM by troupo on Wednesday November 08, 2023 @12:17PM (#38193598)
As wikipedia would say, [citation needed].

Having shipped code in nearly two dozen different programming languages, there were very few times when I wanted my internal parameter name to be different from my external parameter name. The reasons are very simple:

- if your function is simple, you just use the params immediately, like in your `replace` example. It doesn't matter what they are called as long as their names make sense

- if your function is not simple, the parameters are usually immediately transformed into something else (mapped to different data, split to different data etc.), and their original name doesn't matter.

So this leaves a small weird case of "somehow parameter names don't make sense for the caller of the function", and I can't for the life of me come up with such an example (the example with replace in Gleam docs is not convincing, to say the least).

Re:Gleam: a type safe language on the Erlang VM by dylukes on Thursday November 09, 2023 @11:42AM (#38207289)
Two dozen programming languages shipped in or not, almost no languages support this.

It is something that you start appreciating a lot more once you have used it significantly. It helps a lot with writing more self-documenting interfaces that are legible at a glance.

Another detail I did not mention is that _public names are part of the interface_. This means that two functions that differ only in the public names are distinct overloads.

To give an extremely simple example (taken from Apple's docs):

class Counter {

    var count = 0

    func increment() {
        count += 1
    }
    
    func increment(by amount: Int) {
        count += amount
    }
    ...
}

This starts becoming even more valuable when you have more complex objects, and allows you to move (e.g. on some kind of repository class) mangling of overload names into more readable named argument overloads.