Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Lensflare1755486dThose are normally called tuples.
A tuple is a collection of an arbitrary number of values of arbitrary types in a specific order. -
Lensflare1755486d@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.
-
typosaurus1223286dI also prefer to return an dict or object (class instance). Not fond of returning tuples
-
Lensflare1755486d@retoor which language?
For me it depends. Sometimes tuples just make sense. Sometimes a quickly declared struct fits better.
Tuples are practically anonymous structs. -
typosaurus1223286d@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 -
Lensflare1755486d@retoor arrays are not tuples 😂
C and c++ doesn’t have tuples so you are stuck with objects (struct) -
Lensflare1755486d@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. -
typosaurus1223286d@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 -
Lensflare1755486dIt’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.
-
Lensflare1755486d@retoor in js, a tuple is the same as an array because there are no declared types anyway.
C simply doesn’t have tuples. -
Ranchonyx1054786dThis is destructuring. It's not intuitive, but we live with it.
Check MDN for an extensive list of how JS works. -
typosaurus1223286d@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 😁
-
typosaurus1223286d@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' -
Lensflare1755486dThe 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.
(…) -
Lensflare1755486dFor 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.
-
typosaurus1223286d@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 -
AlgoRythm5025986dThis 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!
-
CoreFusionX353284d@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! -
Lensflare1755484d@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. -
typosaurus1223284d@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
-
typosaurus1223283d@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 -
CoreFusionX353283d@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. -
typosaurus1223283d@CoreFusionX ofcourse I don't keep it that way. I had just fun. Happy all tests worked after such refractor
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
question