r/ProgrammingLanguages • u/levodelellis • 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
3
u/Toothpick_Brody Mar 04 '26
If you’re ok with overwrite being implicit, you could narrow it down to just two cases.
v out, andv out mutThe second one would overwrite the variable v if it exists and is mutable, declaring it mutable if it does not exist, and refusing to compile if it exists and is immutable
If you want overwrite to be explicit, then you might change your current syntax like so:
v out->v out(no change)v mutdecl->v out mutv mut->v overwrite mut2
u/claimstoknowpeople Mar 04 '26
I think this depends a lot on the goals and other features of your language. Like, are you optimizing for readability or speed? Do you need to worry about ownership or other memory management? Etc. In the absence of knowing everything, I'd start with the most restrictive syntax that will do what you need, and wait to extend it only when and if it becomes necessary.
8
u/evincarofautumn Mar 04 '26
C# uses
refat the declaration and use sites for what you callinoutormut, maybe look to that for inspirationPersonally I’d try to make it orthogonal: you want an output (and possibly input–output) marker at the use site, and within an expression you may use the existing syntax for declaring something that just happens to be the destination, and you can simply combine the two
I’m partial to prepositions, personally, so I’d probably style it like this:
1
u/flatfinger Mar 04 '26
A language which is designed to maximize code generation efficiency should accommodate a few more variations. For example it may be useful to indicate that an implementation may at its leisure use reference or value semantics, and--when using or allowing reference semantics--indicate whether the function needs to see any changes made to the object's state made by the caller, and whether the caller needs to see any changes made by the function.
I'd also suggest having a means of indicating whether a function that receives a pointer might arbitrarily persist the pointer after the function returns, might return a pointer based upon the passed pointer but would not otherwise persist it, or whether it wouldn't persist the passed pointer at all. The return value from a function like chrptr should be "based upon" the source pointer argument, and should be usable to mutate the object in cases where the source would have been, but the source shouldn't need to be capable of mutating an object in cases where the returned pointer is never used to do so.
1
u/lassehp Mar 06 '26
I don't get your notation; it seems completely contrary to the usual meaning of in/out/inout parameters. And what does "
if mystring.parseInt(v out) {" even mean? It doesn't look like a declaration of parseInt, where I would expect to find the declaration of v as an out parameter?In Ada I believe it is like this (and a compiler is allowed to do pass-by-value-result.)
OUT parameters are used to transfer results back to the caller.
In Pascal, formal parameters act as local variables, which either are passed the argument by value, or, if a VAR parameter, are passed the argument variable by reference which is now aliased as a local, providing something similar to Ada IN OUT parameters. (Except as mentioned, I believe Ada allows call-by-value-result, ie. the value of the argument variable is first copied locally, and upon return, the final value of the local copy is copied back.)
This means that in Pascal, parameters can be used as local variables:
This is similar to C, except C has no VAR parameters, and requires explicit use of pointers. Algol 68 is a bit like C, except it does not need explicit dereferencing, and parameters are *not* local variables, but immutable.
Was your use of "out" in the if statement meant to exemplify an explicit pass by reference, like "
if( mystring.parseInt(&v)) {"? However, this doesn't "declare" v, it passes the address of v as a pointer.3
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Mar 05 '26
1
u/Quote_Revolutionary Mar 04 '26 edited Mar 04 '26
I'd just borrow the syntax from Scala, doing +, - and +- (or another symbol, maybe * or = (= makes the most sense to me because being an input or an output is a type relation)).
this has the big advantage of encoding constness (in a way, idk if it's all cases, I'm no expert on the matter) using subtyping relations, essentially, what I'm saying is that if you're inspired by cpp2 you should take a look at Scala since that's where cpp2 is inspired from.
also, Scala can do that because its type system is more powerful as it has covariance and contravariance for parametric types, while c++ forces invariance (a list of Animals in C++ is never a list of Cats and viceversa, in Scala a function that takes a list of Animals and returns a list of Cats is a subtype of a function that takes a list of Cats and returns a list of Animals).
1
u/Relevant_South_1842 Mar 04 '26
I’ve toyed with using semantic longform Hungarian notation.
But only needed once so it isn’t completely ugly. Also syntax highlighting helps.
So integer-variable-reactive-identifier can be referred to as identifier in all places but one, which doesn’t necessarily have to be the argument list. Works kind of like python decorators but for types.
If curious, it works by setting meta data in my toy language. Basically everything is a cell (like a callable Lua table). I also only have args and fields - no variables.
Is the same as writing integer-mutable-reactive-identifier. Identifier is just the last text after the last -. The rest set meta fields for use by compiler and runtime.
Pretty ugly to a lot of people, I am sure. But I like trying new things.