Live data from Hacker News

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

leandromoreira.com

81–90 of 131 posts

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

#81
post #35

Earlier quoted context omitted.

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…

Variables like "valueSet" scream out that the language lacks a Maybe type instead. One of the worst things about C++ is that it's content to basically not bother improving on the C type system.

C++ has a maybe type. It's called std::optional.

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

#82
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…

This will be in C2Y and is already supported by GCC 15: https://godbolt.org/z/szb5bovxq

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

#83

Earlier quoted context omitted.

Variables like "valueSet" scream out that the language lacks a Maybe type instead. One of the worst things about C++ is that it's content to basically not bother improving on the C type system.

C++ has a maybe type. It's called std::optional.

Here is my experimental maybe type for C: https://godbolt.org/z/YxnsY7Ted

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

#84

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—any type that has methods of appropriate names and signatures is considered to implement the interface, even if its designers were not aware of the interface’s existence. (This involves a small bit of dynamism in the runtime; easily cached, though, as the set of methods of a given type and the set of all in…

I don’t agree it’s a structural VS nominal difference. Typescript is structural, but it does have the “implements” keyword. Which makes a million times more sense to me, because realistically when do you ever have a structure that usefully implements an interface without being aware of it?? The common use-case is to implement an existing interface (in which case might as well enforce adherence to the interface at dec…

TypeScript doesn't require a class to use it, though, because it's structurally typed. All that "implements Foo" in this example does is make sure that you get a type error on the definition of "One" if it doesn't have the members of "Foo".

If "Two" didn't have a "name: string" member, then the error would be on the call to "test".

    interface Foo {
        name: string
    }

    class One implements Foo {
        constructor(public name: string) {}
    }

    class Two {
        constructor(public name: string) {}
    }

    function test(thing: Foo): void {
        //...
    }

    test(new One('joe'));
    test(new Two('jane'));

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

#85
post #78
post #67

Earlier quoted context omitted.

> 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.

Well written idiomatic C is certainly not valid C++.

Depends on the beholder, however it hardly matters on the days of C23, as mentioned.

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

#86
post #62
post #35

Earlier quoted context omitted.

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…

Declaring a variable in a loop or if statement is supported since C99: https://en.wikipedia.org/wiki/C99

Also in Java: https://www.geeksforgeeks.org/for-loop-java-important-points...

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

#87

For the record, this design pattern is called a virtual method table, or vtable. I'm surprised that this article never mentioned the term. C++ programmers will know this pattern from the `virtual` keyword.

I learned it from a textbook. I think it was an earlier printing of https://docs.freebsd.org/en/books/design-44bsd/

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

#89
post #28

The language is called Go. Other than that, yeah doing by hand what C++ and Objective-C do automatically.

For one, ffmpeg is 9 years older than Go. Plus, when dealing with video files a garbage collected language probably isn't going to cut it. C++ and Obj-C also feel overkill for ffmpeg.

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

#90

Earlier quoted context omitted.

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 pr…

[deleted]
Post reply on HN