r/ProgrammingLanguages • • Mar 04 '26

Out params in functions

I'm redesigning the syntax for my language, but I won't be writing the compiler anytime soon

I'm having trouble with naming a few things. The first line is clear, but is the second? I think so

myfunc(in int a, inout int b, out int c)
myfunc(int a, int b mut, int c out)

Lets use parse int as an example. Here the out keyword declares v as an immutable int

if mystring.parseInt(v out) {
    sum += v
} else {
    print("Invalid int")
}

However, I find there's 3 situations for out variables. If I want to declare them (like the above), if I want to declare it and have it mutable, and if I want to overwrite a variable
What kind of syntax should I be using? I came up with the following

mystring.parse(v out) // decl immutable
mystring.parse(v mutdecl) // decl mutable
mystring.parse(v mut) // overwrite a mutable variable, consistent with mut being inout 

Any thoughts? Naming is hard

I also had a tuple question yesterday. I may have to revise it to be the below. Only b must exist in this assignment

a, b mut, c mutdecl = 1, 2, 3 // mutdecl is a bit long but fine?

The simple version when all 3 variables are the same is

a, b, c = 1, 2, 3   // all 3 variables declared as immutable
a, b, c := 1, 2, 3  // all 3 variables declared as mutable
a, b, c .= 1, 2, 3  // all 3 variables must exist and be mutable
11 Upvotes

19 comments sorted by

View all comments

Show parent comment

1

u/levodelellis Mar 06 '26

Don't worry about it. But I'll answer anyway

Using C++ syntax, inside "class string" I'd have a bool parseInt(int64_t& v).
In my own syntax (I like return values on the right), it'd be parseInt(int v out) bool where out means it will not read the variable (so it's allowed to be uninitialized), it will write to it. Since I dislike line noise I liked

if (mysz.parseInt(v out)) { /* v is newly declared */

which would have v be whatever was defined in parseInt (so int64_t in C++ syntax, but 'int' in mine)
For the moment I'm using mutdecl to declare as mutable and mut to say overwrite an existing variable

I like some of the suggestions. Like having mut and out separate. So

1

u/lassehp Mar 06 '26 edited Mar 06 '26

Oh, I don't worry, I am just curious and interested! :-)

I think I get your notation now.

so you have

String.parseInt(v out int) bool {
  # if this parses as an integer, set v to the int value
  # and return true
  # v is assignable inside the body of parseInt.
  ...
  if error { return false }
  v = numberval;
  return true;
}

...
s String;
i mut int;
if s.parseInt(i) {
  # the mutable i from the outer scope now is
  # set to the value parsed. It is still a mutable int variable
  # here in the condition branch
}

# alternatively A:
if s.parseInt(j out) {
  # j is declared as the out type of parseInt,
  # which happens to be int. It receives the value,
  # but is not mutable here inside the condition branch
}

# or even more alternatively B:
if s.parseInt(j mut out) {
  # j is declared as a mutable var of the out type of parseInt.
  # here it would be allowed to further assign to j
  ...
  j := j*128
  ...
}

# or alternative C with explicit typing
if s.parseInt(j int) {
  # this would declare j as a local immutable int, initialised
  # with the out value v of parseInt, but explicitly declared
  # as an int
  # j mut int would be the same as j mut out, but with explicit
  # type instead of inferred from parseInt declaration.
  # parseInt does not need to care if the receiving variable
  # "becomes" immutable or not in the caller.
}

I may still be misunderstanding what you are looking for, but the way I see it now, is that if you don't want the local recipient of the out value from parseInt to exist outside the scope where it is used, you could use one of the three alternatives to combine a local declaration with the reception of the value, and either have the local receptor declared as a mutable or immutable variable, even though it is first passed to parseInt as a variable reference.

In kind-of-C it might be something like below (switching from member-dot-notation method to a function with the string as the first argument):

{ int _tmp;
  if(parseInt(s, &_tmp)) {
    const int j = _tmp;
    ... // here the local j is "immutable"
  }
}

but of course in C it would be difficult to declare the local without an explicit type, as there is no typeof_param(parseInt, 2) to give you the type of the second parameter of parseInt.

I don't know if this is what you intended, but I find it interesting to think about how to do type inference for pass-by-reference parameters like this seems to be.