Live data from Hacker News

Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

leandromoreira.com

61–70 of 131 posts

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#61

Earlier quoted context omitted.

>The difference between Go and Java is that in Go a type need not declare its adherence to an interface up front. Go can't declare adherence up front, and in my view that’s a problem. Most of the time, explicitly stating your intent is best, for both humans reading the code and tools analyzing it. That said, structural typing has its moments, like when you need type-safe bridging without extra boilerplate.

One of the main uses for interfaces in Go is defining the contact for _your_ dependencies. Rather than saying your function takes a socket, if you only ever call Write(), you can take a Writer, or define another interface that is only the set of functions you need. This is far more powerful than declaring that your type implements an interface up front. It allows for things like e.g. multiple image libraries to imple…

> It allows for things like e.g. multiple image libraries to implement your interface without knowing it

That virtually never happens. Seriously, what would be the odds? It’s so much more usual to purposefully implement an interface (eg a small wrapper the writer thingy that has the expected interface) than to use something that happens to fit the expected interface by pure chance.

It’s not a structural vs nominal problem but other, typescript is structural but has the implements keyword so that the interface compliance is checked at declaration, not at the point of use. You don’t have to use it and it will work just like Go, but I found that in 99% of cases it’s what I want: the whole point of me writing this class is because I need an interface implementation, might as well enforce it at this point.

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#62
post #35

Earlier quoted context omitted.

> weight and inconsistencies of (early) C++ Since very little is ever removed from C++, all the inconsistencies in C++ are still there.

Every language has inconsistencies, and C is not stranger to that. Much of c++’s baggage is due to C and you carry the same weight. That’s not to say that initialization isn’t broken in C++, but just like many features in many languages (off the top of my head in C - strcpy, sprintf, ctime are like hand grenades with the pin pre pulled for you) don’t use them. There’s a subset of C++17 that to me solves so many issue…

For C users. And C++ users:

In C++ we can declare variable in the while or if statement:

https://en.cppreference.com/w/cpp/language/while

https://en.cppreference.com/w/cpp/language/if

It's value is the value of the decision. This is not possible with C [1].

Since C++17 the if condition can contain an initializer: Ctrl+F if statements with initializer

https://en.cppreference.com/w/cpp/language/if

Which sounds like the same? Now you can declare a variable and it value is not directly evaluated, you also can compare it in a condition. I think both are neat features of C++, without adding complexity.

[1] Also not possible with Java.

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#63

I spend a ton of time in FFmpeg, and I’m still blown away by how it uses abstractions to stay modular—especially for a project that’s been around forever and still feels so relevant. Those filtergraphs pulling off polymorphism-like tricks in C? It’s such an elegant way to manage complex pipelines. e.g. ffmpeg -i input.wav -filter_complex " [0:a]asplit=2[a1][a2]; [a1]lowpass=f=500[a1_low]; [a2]highpass=f=500[a2_high];…

how similar are the C abstractions in ffmpeg and qemu given they were started by the same person?

QEMU's abstractions were added when Fabrice was almost completely inactive already (starting in 2008 for some command line support).

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#65
post #45

Earlier quoted context omitted.

it actually isn't https://www.stroustrup.com/bs_faq.html#C-is-subset

Do you usually post links without reading them? "In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C. Every C program can be written in essentially the same way in C++ with the same run-time and space efficiency. It…

Do you usually post links without reading them?

all the time -- I call it "crowd-sourcing intelligence" :-)

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#66

Earlier quoted context omitted.

Shouldn’t this be named phenomenal rather than structural ? In both cases there is a structure assumed, but one is implicitly inferred while the other one is explicitly required.

I think you’re making a joke, but in Go you get both. You can have the compiler enforce that you implement an interface with a simple declaration. Most people do.

No intended joke in that case, but it’s nice to have the feedback it might seen as if it was.

I don’t know Go to be frank, just had a very shallow look at it once because of an interview, and apart big names behind it, it didn’t shine in any obvious way — but that’s also maybe aligned with the "boring" tech label it seems associated with (that is, in positive manner for those who praise it).

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#67
post #60
post #45

Earlier quoted context omitted.

Do you usually post links without reading them? "In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++. However, C++ supports every programming technique supported by C. Every C program can be written in essentially the same way in C++ with the same run-time and space efficiency. It…

> For example, every example in Kernighan & Ritchie: "The C Programming Language (2nd Edition)" is also a C++ program. " That is rather dated, they do things like explicitly cast the void* pointer returned by malloc, but point out in the appendix that ANSI C dropped the cast requirement for pointer conversions involving void , C++ does not allow implicit void conversions to this day.

> Well written C tends to be legal C++ also

The "well written" remark is relevant.

Many style guides will consider implicit void conversions not well written C.

Naturally we are now on C23, and almost every C developer considers language extensions as being C, so whatever.

Re: Exploring Polymorphism in C: Lessons from Linux and FFmpeg's Code Design (2019)

#70
post #14

Earlier quoted context omitted.

I haven’t worked with ffmpeg’s code, but I have worked with QEMU. QEMU has a lot of OOP (implemented in C obviously) that is supported by macros and GCC extensions. I definitely think it would have been better (and the code would be easier to work with) to use C++ rather than roll your own object model in C, but QEMU is quite old so it’s somewhat understandable. I say that as someone who mostly writes C and generally…

What's the reason for ffmpeg to use C, also historic?

C has less moving parts — it’s more difficult to define a subset of C++ that actually works across all platforms featuring a C++ compiler, not to mention of all the binary-incompatible versions of the C++ standard library that tend to exist — and C is supported on a wider variety of platforms. If you want to maximize portability, C is the way to go, and you run into much fewer problems.
Post reply on HN