r/AskProgramming • • Sep 21 '26

Other What is a niche feature or implementation that you love using in your go-to language?

For me, I love C++'s std::queue. Ridiculously simple, can only use the front, but so powerful.

8 Upvotes

50 comments sorted by

28

u/Soft-Marionberry-853 Sep 21 '26

You know how people instinctively give preferential treatment to people that they find attractive even if it's not earned. That is how I treat languages that let functions return multiple values.

I dont know how much of my professional life has been wasted returning an array or making an object that just holds 2 integers. And it never made any sense, why does a function let you pass it as many values as you want but nope, you can only return 1 value.

3

u/vivaidris Sep 21 '26

Most languages nowadays have their own tuple type, mostly for value/error, but can be used for multiple values.

14

u/generally_unsuitable Sep 21 '26 edited Sep 21 '26

Because of how function calling and returning works at the hardware level.

Pushing data to the stack has always been meant to be a multiple-variable process, because processes are generally controlled by multiple variables. So, the stack works in a way, and the standards have been developed in such a way, that this usage is encouraged and made simple.

However, standards have generally conformed to the way people write assembly.

It's generally expected that you return a single value in a specific register. In x86, this is EAX. In PIC, it's w_reg. And so forth. The alternative is to pack the stack with return values, or pack the general purpose registers, and either of the options have rippling problems.

The general rule of thumb is that when you return from a function call, the stack should be just as you found it. Also, when you return from a function call, you should have restored any changes you have made to the general purpose registers, except for EAX, or whatever your architecture standard is.

11

u/Otherwise_Monitor856 Sep 21 '26

that sounds correct, but you could always return a struct from a function C.

It doesn't need to fit a register. The premiss that functions are designed to return only one value because of registers is wrong.

The reason we need a struct to return multiple is so that you can write something to address the values. It's the notation that's annoying, but it works.

In modern C++, you can do this:

std::tuple<int, double, std::string> getData(){
return {42, 3.14, "hello"};
}
auto [i, d, s] = getData();

-16

u/generally_unsuitable Sep 21 '26

Imagine writing code that ugly and then sharing it . . .

7

u/Soft-Marionberry-853 Sep 21 '26

jfc dude.

-3

u/generally_unsuitable Sep 21 '26

Relax, guy. It's template C++. It's ugly. That's life.

2

u/UnrealHallucinator Sep 22 '26

They're downvoting you but you're right hahaha

2

u/generally_unsuitable Sep 22 '26

^ This guy gets it.

5

u/BranchLatter4294 Sep 21 '26

Also has to do with the mathematical foundation of programming where a function can only return one value.

3

u/Soft-Marionberry-853 Sep 21 '26 edited Sep 21 '26

Otherwise_Monitor856, max123246 Ive been in the field for decades its nice to be finding new things to learn. Thank you all.

2

u/snaphat Sep 21 '26

I would say it's more of how it works on a calling convention level than a hardware level. A calling convention could just as easily designate many return registers.

Historically, in various architectures you had only a few registers (accumulator, x, y) but things could still just be pushed to the stack or stored in memory and pulled after returning. There's nothing to preclude that generally speaking.

The rationale imo more likely ordinates from the mathematical notion of a function. This is explicitly the notion used in Lisp for example. Similarly, in the seminal work on Fortran, it specifically describes the goal to allow procedures to be expressed in a language like that of mathematics and has literal examples of mathematical functions essentially transcribed in Fortran.

Ironically, however, the earliest high level language, Plankalkül, apparently allowed multiple outputs unlike the later formal languages mentioned above 

1

u/generally_unsuitable Sep 21 '26

 but things could still just be pushed to the stack or stored in memory and pulled after returning.

How would you return multiple values to the caller if you're expected to return the stack as you found it and to never modify stack that isn't yours? And, in what way would putting values into static memory be a "return"? That's equivalent to receiving a pointer and putting a value there through indirection.

All of this is very different from how a return instruction works in most architectures, which is basically moving a return vector into the program counter and putting a value in a working register simultaneously.

3

u/Otherwise_Monitor856 Sep 21 '26 edited Sep 21 '26

How would you return multiple values to the caller if you're expected to return the stack as you found it and to never modify stack that isn't yours?

The C standard supports returning structures of any size from a function.

If a function is returning a struct, it's returning multiple values and that means function returns have never been limited to having to fit in registers.

When the function returns a struct, the Microsoft ABI will have the caller pass the pointer to the struct in a register, which is a pointer to a preallocated area by the caller. That area is going to be on the stack, in practice!

My point is that the languages are unbothered by returning 1 or many values; the hardware implementation doesn't inflence it. Functions just typically return one variable of simple type by convension and style, not because there was a limitation around it due to the hardware

2

u/snaphat Sep 21 '26

I think there are two separate things being conflated here: the CPU's return mechanism and the ABI/calling convention. I'll address the latter first and the former second.

How would you return multiple values to the caller if you're expected to return the stack as you found it and to never modify stack that isn't yours?

Depending on the calling convention, a function need not leave the stack contents untouched. That's just part of the caller-callee ABI contract.

For example, if we defined our own x86 ABI, we could designate multiple registers for results and have the caller reserve stack space for additional results, and the callee could write those results into agreed-upon slots for the caller to read afterward. Conceptually, that's not much different from how the SysV AMD64 ABI passes the first six integer arguments in registers and additional arguments on the stack. It's just in the opposite direction.

And, in what way would putting values into static memory be a "return"? That's equivalent to receiving a pointer and putting a value there through indirection.

That's really neither here nor there. It was just an example showing that storing multiple results in memory isn't prohibited by the hardware. And fixed static storage wouldn't necessarily involve receiving a pointer anyway; both sides could simply agree on the locations.

All of this is very different from how a return instruction works in most architectures, which is basically moving a return vector into the program counter and putting a value in a working register simultaneously.

This isn't generally true. In most architectures, return values are separate from the control-flow return. Instructions that explicitly combine the operations are comparatively rare

In x86, the retinstruction doesn't return a value, it just transfers control back to the saved return address. The callee is responsible for placing the value in EAX/RAX. RAX is used for the return value because the ABI contract says the callee must store the return value there. An ABI could just as easily designate RCX andRDX as additional return registers. There's no hardware limitation/restriction on this.

In ARM64, the ret instruction similarly doesn't return a value. The callee is again responsible for placing the result in X0 and we could just as easily define an ABI that used additional registers for additional return values.

It's the same deal on the PowerPC/Power, MIPS, etc.

If we want to talk about embedded architectures' from yester-year, the 6502, 68HC11, TI*, PDP-11, and Z80 have the same separation between returned data and control-flow.

In fact, in writing all of the above, I couldn't even think of early families with the operations combined outside of PIC. Hadn't even occurred to me that even historically it was kind of rare.

Upon checking, Intel 4004 has a combined instruction, but the Intel 8008 from the following year does not. So, one could argue that the first commercially available 4-bit microprocessor has a combined operation, but that the first commercially available 8-bit microprocessor does not.

2

u/generally_unsuitable Sep 24 '26

I did all my hardcore asm years in PIC. Thanks for the detailed clarification.

1

u/code-garden Sep 24 '26

The caller can reserve stack space for the return values and pass a pointer to the start of that space as an extra parameter to the function. This is the same as how large struct returns usually work.

1

u/generally_unsuitable Sep 24 '26

If you're going to do that, though, why not just use the heap and avoid the danger.

1

u/snaphat Sep 24 '26

Idk why they said pass a pointer. You don't need to do that part assuming it's defined as part of an ABI. You have the stack pointer implicitly so you just reserve the stack space using it and offset into it for stores in the callee

And as for why? It's cheaper and not inherently dangerous because the assumption would be that it is implicitly handled correctly by the compiler machinery, just like how all other ABI details are

3

u/max123246 Sep 21 '26

Risc-V has multiple registers that can be used as return values. Single return values isn't something inherent to assembly, only x86. And I won't really pay mind to the assembly language that for the longest time had a single register used to store the result of adding numbers

2

u/generally_unsuitable Sep 21 '26

And MIPS has 32 registers and like 28 of them could be used to return values, if you wanted. But people don't return 28 values in the GPRs.

X86 has had multiple GPRs for decades now, but the standard has remained.

1

u/soundman32 Sep 21 '26

Sounds like you want to return a Tuple, which many languages support, and the good compilers will put a few return values into registers before automatically switching to a heap object when it gets too big.

5

u/qrzychu69 Sep 21 '26

Computation expressions in F#, especially the custom ones. You can have "effectful" binds to variables, and what they do depends on which builder you have.

let! a = doStuff () will await it in task, but will do replicate the ? operator from Rust in result builder. And then you can get taskResult that will do both.

Another one is also dotnet, but supports C# - expression trees.

You can do blogs.Where(x => x.Comments.Count > 5), that's obvious for everyone. But you can also do database.Blogs.Where(x => x.Comments.Count > 5).Select(x => new { Title = x.Title, Id = x.Id}). At runtime this get converted into expression tree, and the builder can translate your code to SQL, and it will only select what you actually need.

What's even better, you can also build those expressions dynamically at runtime, and both the SQL part (called EF Core for those curious, best ORM out there IMO) and the expression part support anonymous records.

That means you can basically do services.AddGraphQL<MyDatabaseModel>() and yes, it works just how you think, with paging, sorting, projecti, selecting only what you need - everything. You start with that, add some customization for auth, hide some tables, throw in some projections, add couple mutations - you get full graphql service generated for you. Sometimes that's all you need.

All that works because expression trees can be created on the fly, and they can be translated to SQL, and there is reflection so you can query the structure of your classes.

It's an amazing piece of tech

1

u/vivaidris Sep 21 '26

That is pretty sick. Seems really useful.

8

u/generally_unsuitable Sep 21 '26

I think I wrote C for a decade before I realized that sprintf() returns the character count of the generated string. In embedded, this saves so much time it's ridiculous.

5

u/Ok_For_Free Sep 21 '26

In JavaScript, reflection is basically free.

3

u/soundman32 Sep 21 '26

It's free because everything is really slow. In proper languages, you don't pay for things you don't use, but in JS, you pay for everything even if you don't use it.

3

u/garagedragon Sep 21 '26

So Rust has so many nice features, but here's a couple of more obscure ones.

You can pattern match multiple variants of an enum at once, so long all the names you assign have consistent types in every variant. This means if you have Result<T, T>, you can say Ok(val) | Err(val) => ... in one line. This works even if the fields are named, so long as you discard any fields that don't show up in all branches, i.e. you can say Foo { name,..} | Bar { name, ..} => ... if both variants have a field that's the same type

Also, Option and Result are IntoIterable, which means among other things, if you have an iterable of them, you can flatten() it to get only the successes. It also means that you can collect() an iterable of Result<T, E> into a Result<Vec<T>, E> which gives you either a vec of the success cases, or the first error.

3

u/ern0plus4 Sep 21 '26

Rust enum - just love it.

4

u/engineerFWSWHW Sep 21 '26

I love LINQ on .NET/C#.

2

u/Blumingo Sep 23 '26

It's amazing

2

u/simonsanchezart Sep 21 '26

`next(generator, None)` in python to get the first element from a collection/generator that matches a condition

2

u/azimux Sep 21 '26

I love Ruby's blocks and (almost) everything about them. I think they were a really killer idea building on Smalltalk's blocks.

2

u/ExtraTNT Sep 21 '26

maybe

Haskell

1

u/lgastako Sep 22 '26

Maybe is cool, but for Haskell I'd go with lenses.

1

u/ExtraTNT Sep 22 '26

Used them a few times, to increase readability…

2

u/Eleventhousand Sep 21 '26

Default properties in Visual Basic

1

u/gnunn1 Sep 21 '26

I'm not sure how niche it is, but I liked D's templatemixin feature.

https://dlang.org/spec/template-mixin.html

2

u/azimux Sep 21 '26

I've never used D but something I think about sometimes is how it has a `pure` keyword where you can do imperative stuff within the function as long as it's contained. I don't know if I remember that correctly or if that's accurate, but feels like that would be niche and a cool idea to me!

1

u/Daemontatox Sep 21 '26

C++ splice() with lists

1

u/Dunkelziffer17 Sep 21 '26

Xtend collection literals are really nice.

val list = #['foo', 'bar'] val set = #{'foo', 'bar'} val map = #{'foo' -> 42, 'bar' -> 23}

Other languages introduced similar constructs, of course, but I still find Xtend’s syntax incredibly concise.

1

u/marrsd Sep 21 '26

Some dynamically typed languages allow you to swap the data from under an object at runtime. For example, you could replace a module imported by another module you're testing rather than set up dependency injection to pass in mocks and stubs.

1

u/publicOwl Sep 21 '26

I miss case classes in Scala. Pattern matching, all the stuff it gives you for free…

1

u/Pzzlrr Sep 21 '26

DCGs in prolog are so nice.

1

u/Haunting_Stuff5104 Sep 22 '26

returning multiple values is the only thing keeping me sane. I will never write a struct just to hold two integers again

1

u/Astro-2004 Sep 28 '26

Go defer is a game changer in how I clean all the mess that I get when dealing with I/O or concurrent software

1

u/Darkschlong Sep 21 '26

Scanner sc = new Scanner(system.in);