6 ms·
The whole C interface is clunky because C does not support multiple return values. More advanced languages should not be forced to deal with it.
by miohtama 7y ago
The whole C interface is clunky because C does not support multiple return values. More advanced languages should not be forced to deal with it.
- smitty1e 7y ago"Should" is as wobbly as errno, no?
- masklinn 7y ago> 3. SHOULD This word, or the adjective "RECOMMENDED", mean that there may exist valid reasons in particular circumstances to ignore a particular item, but the full implications must be understood and carefully weighed before choosing a different course.
- smitty1e 7y agoThat seems a defensible definition, but also at odds with your usage: > More advanced languages should not be forced to deal with it (I'm more in agreement with the definition you offer than your usage.)
- dbtx 7y agoJust a tick-- the quoted usage and the definition appear to be from two different people.
- guerrilla 7y agoAre you sure its not the other way around? Might C not have return values because of the systems it ran on? How would that work today in Linux, for example? Map a page to have some memory shared between the kernel and userspace?
- masklinn 7y ago> The whole C interface is clunky because C does not support multiple return values. C has pointers which can easily enough be used as "out parameteters". In fact the C standard library regularly makes use of these. It's not amazing, but it works fine, and it's usually fairly easy to go back and forth e.g. expressing a stat(2) call is reasonably straightforward.
- jononor 7y agoHowever there is no way to know that programatically, and no standard way to annotate it either. So in FFI / language bindings context this is really tedious to work with.
- merb 7y agowell most of the time the return value is just an error integer, while the real return value is a pointer parameter.
- moomin 7y agoMy hot take here is that multiple return values are a conceptually complex way of handling tuple return values. The C API could do this, it just doesn’t.
- rwmj 7y agoYou can return structs from C functions (note, I don't mean pointers, I mean whole structs). You can pass in structs too. It's not commonly done and is a bit clunky compared to returning a tuple, but it is possible.
- masklinn 7y ago> It's not commonly done It’s really rather common eg several time or socket functions take a pointer as « output parameter », fill it and return an error code. Not the best but not the worst either.
- chewxy 7y agoWhat's the difference? My understanding is as follows: A struct is a tuple. It's just some structured memory. Where a tuple would be indexed numerically, a struct is indexed with names of the fields (equiv in Python to NamedTuple)
- rwmj 7y agoWhat happens under the hood would be the same, but in C you have to define a new struct for each return type which is clunky. In ML or Python you can return ad hoc tuples, eg: let res, _, _ = fun_which_returns_3_tuple () vs struct my_result { int res; int other; int third; }; ... struct my_result r = fun_which_returns_struct (); printf ("%d\n", r.res);
- deleted 7y ago[deleted]
- jacquesm 7y agoTuples can pull their contents from different memory locations whereas structs are laid out continuously in memory (modulo padding if enabled). Of course you could store pointers in the structs you return but that would defeat the whole 'single return' concept, then you may as well return a pointer to a struct which is ugly because it will never be thread safe and is a particularly nice pitfall regarding ownership of that struct. So either return the whole struct as values or, alternatively - and this is the way most library functions do it - call the function with a pointer to an empty struct which the function then fills. That leaves the return value for error status.
- justincormack 7y agoThe Unix API even returned a pair of file descriptors for the pipe syscall. Most ISAs have a way of returning multiple values eg for structs by value. However they fell out of fashion, I think only NetBSD still has a multiple return pipe on some architectures.
- mannykannot 7y agoThe issue here is the interface to the operating system, which should be as universal as is feasible. Having multiple return values would work against that, just as using macros does.