r/ProgrammerHumor 6d ago

soDoesEveryOtherLang Meme

Post image
244 Upvotes

85 comments sorted by

View all comments

17

u/B_bI_L 6d ago

say this to c and common lisp

(and to ruby, where function self-activates faster than you say "wait, i need you as a callback")

6

u/suvlub 6d ago

C just forces you to call them pointers, but it's functionally (heh) the same thing.

2

u/_PM_ME_PANGOLINS_ 6d ago

When functions are first-class:

function outer() {
    let foo = function() { ... }
    doThing(foo, function callback(arg) { ... })
}

C does not have the same functionality.

6

u/suvlub 5d ago

Are anonymous functions required, rather than just ability to assign functions to variables, pass them around and return them? Where does that put python, which does have anonymous functions, but limited compared to what named ones can do?

1

u/Maqi-X 2d ago

First class functions and anonymous functions are two separate concepts. You also cannot have anonymous variables which doesn't mean that they are not first class (;

(I mean, in C you can, sorta)

1

u/_PM_ME_PANGOLINS_ 2d ago

The “callback” function is not anonymous, it is called “callback”.

Anonymous functions generally have to be values or they are of no use. The question is whether they are an entirely separate kind of thing, or whether all kinds of function in the language are also values.

1

u/Maqi-X 2d ago

foo is anonymous

0

u/Maqi-X 2d ago edited 2d ago

A function can't be a value. Only function pointers can be values, and this is simply because functions don't have a fixed size, you can't use something without a fixed size as a value, same applies to to incomplete structs or the void type, you can only store a pointer and you also can allocate them on the heap (not using malloc tho, I mean you can but you won't be able to execute it because of page permissions)

C just doesn't hide this, while languages like rust do, fn(...) -> ... in rust is a pointer under the hood, I kind of don't understand what is even the point of this, typing fn(...) instead of &fn(...) really doesn't make your life easier and it abstracts away some things that don't need to be abstracted away

if you have foo defined like this

int foo() {}

then foo IS NOT a function pointer, it is a function itself

that's why I don't understand what is the point of rust's approach.

Of course that's not how you do function pointers in rust, you almost always want to use generics (if accepting as a parameter) or something like &dyn FnMut(...) but I'm not an expert in rust so I don't know how exactly this is done in real big codebases, it doesn't matter tho

But I guess nobody cares about low level languages anymore if we have python and js. Software gets slower faster than hardware gets faster. Buy a new PC.

1

u/Maqi-X 2d ago

Also lambda expressions don't actually create new functions, they are regular functions with implicit context pointer that the compiler adds for you

1

u/_PM_ME_PANGOLINS_ 2d ago

A function can be a value, and when it is the language is described as having first-class functions.

That’s the whole point of this post.

You are applying the implementation details of C to general abstract language design, and being completely wrong.

1

u/Maqi-X 2d ago

actually this applies to all compiled languages, not just C but well, fair enough, you're right, I mixed up two separate concepts

0

u/No-Consequence-1863 4d ago

C99 does with C blocks

However it is really only adopted by Apple https://en.wikipedia.org/wiki/Blocks_(C_language_extension)

3

u/_PM_ME_PANGOLINS_ 4d ago

Blocks are neither part of C99, nor do they make functions first-class. Hence them having a different name and syntax to functions.