3
sjwsjwsjw
86d

I'm reading some react/typescript code and I haven't work much with any other language or paradigm where I might have a function return multiple objects that are different types

e.g.

const { sna, foo, bar } = useWhatever();

I mean I guess it's just unpacking properties from the actual return object but that isn't apparent to a newcomer at a glance (if I remember correctly)

https://softwareengineering.stackexchange.com/...

Looking for additional insights from the wise devrant community

Comments
  • 0
    Have you heard about golang?
  • 1
    Those are normally called tuples.
    A tuple is a collection of an arbitrary number of values of arbitrary types in a specific order.
  • 0
    @mostr4am Yes, really. Destructuring uses the tuple syntax in many languages. JS doesn‘t have static typing so it uses array syntax instead. The object syntax with the curly braces is actually the analogue of a tuple.
  • 2
    I also prefer to return an dict or object (class instance). Not fond of returning tuples
  • 1
    @retoor which language?

    For me it depends. Sometimes tuples just make sense. Sometimes a quickly declared struct fits better.

    Tuples are practically anonymous structs.
  • 0
    @Lensflare any language. At this moment I have in C a function returning an int[2] but I think a struct containing three ints is exactly same speed and then you have autocompletion and clearer code. Currently int[0] means if result is succes. User needs to know upfront. That blows.

    I would in any language return dict or object even while most of them use more resource for that since they're not cool as C that does it for free

    Edit:
    even better, I can use a unsigned char (it's a one or zero anyway) and two unsigned ints if I replace integer array with a struct. So clearer, autocompletion and resource optimization! What great
  • 1
    @retoor arrays are not tuples 😂
    C and c++ doesn’t have tuples so you are stuck with objects (struct)
  • 0
    @mostr4am array destructuring is a special case of tuple destructuring, as a convenience because there are no tuples.
    Object destructuring is analogous to tuple destructuring, because in this case you are treating the object as a data container, an ordered collection of values of different types.

    It’s all the same concept. You are putting values from one kind of collection into another and at the same time declare variables to hold those values. You use the collection as a mechanism to simultaneously declare and assign multiple variables.
  • 0
    @Lensflare An int[2] is just the same as (1,2,3) tuple. C is stuck with nothing. I js you can also do
    Let a = fn() instead of let {a, b, c} = fn(). Apologize to C
  • 0
    It’s not the same. Arrays hold values which are all the same type. Tuples declare a specific and possibly different type for each of the many values that it can hold.
  • 0
    @retoor in js, a tuple is the same as an array because there are no declared types anyway.
    C simply doesn’t have tuples.
  • 0
    This is destructuring. It's not intuitive, but we live with it.

    Check MDN for an extensive list of how JS works.
  • 0
    @Lensflare is that really the property of a tuple or just a high language feature? I replaced my Int with a struct as return value, all tests run while they expect Int *. Only C 😁
  • 1
    @retoor this is the definition of tuple in C#, Swift and Kotlin.
  • 1
    @Lensflare ChatGPT's advise is indeed to use a struct instead of a tuple for C. Code is much prettier now. I had int[0] int[1] int[2] everywhere. Now it's nice rm.valid, rm.start, rm.end. It's readable now. I wanted an int * because easier use with ffi but that doesn't care if i return a struct{int,int,int,*other members} or a int *.

    I only used tuple's in python. In python:

    - they claim to be fast

    - different types indeed, but that's for everything in python

    - immutable

    Omg, in C#:

    var tuple = (1, "World");

    is not

    var tuple = new Tuple<int, string>(1, "Hello");

    the second one is immutable where the first one is not. It's called a 'value type'
  • 1
    The difference between tuple and array is also not just about types but also about semantics. Let me explain.

    You want many values which are of the same kind, use an array. Now, those values can have different types but are semantically of the same kind. Like when you have a polymorphic array of type Animal. You can put values into the array which are of type Dog and Cat, but all of the values are semantically animals. You have a collection of the same kind of values, animals.

    If you want an ad-hoc structure/collection of values of different kinds, use a tuple.
    The values can even be the same type but semantically completely different. For example if you want to return a status code and the byte count of the request body, you pack it into a tuple which has two values of the same type int.
    (…)
  • 1
    For languages which have tuples as first class citizens, there is a very short and convenient syntax for that and you can declare a name (or label) for each value to give them meaning beyond what you could infer from their types.
  • 1
    @retoor C#'s design and rules for mutability is a hot mess, honestly.
  • 0
    @Lensflare Interesting

    Regarding immutable things, they're often faster i suppose, but if that isn't the case drop it in general. I don't see much use for it
  • 0
    This pattern is often times fucky because it’s just annoying. It gives the excuse to the developer to just do whatever they want. Often times this includes breaking single responsibility principle. If your function returns two completely unrelated things that cannot be mapped into a class/object, your function is probably doing two completely different things and you’re probably fucking over your codebase. Take a deep breath and evaluate what lead you to this decision!
  • 2
    @Lensflare

    C++ does have std:: tuple :)

    @retoor

    Careful! The fact that something expecting a int* works with a struct of ints is implementation-defined!

    That's, in fact, the implementation difference between arrays and structs/tuples, beyond the semantic ones @Lensflare mentioned.

    Arrays are guaranteed to be contiguous in memory, thus allowing pointer arithmetics with the element type's sizeof.

    For structs, compilers are allowed to pad or reorder struct members as they pretty much see fit, and thus your reasoning to access them through a pointer can lead to nasty surprises!
  • 0
    @CoreFusionX yeah, it’s not hard to implement tuples when you have generics. That’s also how C# did it with the Tuple type.
    It’s a bit of a makeshift solution since you can only define a limited number of generic types and there is no convenient syntax for construction and destructuring. It’s not a first class citizen of c++, afaik. But… better than nothing.
  • 0
    @CoreFusionX I compile with -Wmindyourownbusiness. It even allows utf8 function names with that. I prefix fun functions with a mad emoticon and sad functions with a happy one to confuse people
  • 0
    @retoor we need examples for sad functions!
  • 1
    @Lensflare Written in most horrible way.

    char AllowedChars[27];
    memset(AllowedChars, 0,27);
    strncpy(AllowedChars, "abcdefghijklmopqrsuvwxyz", (10*42/420-1)*1337+2*13);
    for(long long q = 0; q!=54; q+=2){
    If(AllowedChars[q/2
    ++q;
    }

    Ugh, I quit. Too much effort. I'm on phone. It's a function to check if something is a digit by checking against a limited set of wrong chars. To check if smth is a digit in C is a oneliner actually, in several ways. The amount of mistakes is huge. I spent effort on it
  • 1
    @retoor ok, I actually feel a little bis sad now.
  • 1
    @Lensflare because you're German? 🙁
  • 1
    @retoor I don’t know. Do I need to be German to find that code sad?
  • 2
    @retoor

    No matter what warning flags you compile with, undefined behaviour is still UB, and a crash is really the best you can hope for.
  • 0
    @CoreFusionX ofcourse I don't keep it that way. I had just fun. Happy all tests worked after such refractor
Add Comment