top | item 46615766

(no title)

K0nserv | 1 month ago

> In c the callers isn’t choosing typically. The author of some library or api decides this for you.

Tbf this applies to Rust too. If the author writes

   fn foo(bar: Box<dyn BarTrait>)
they have forced the caller into dynamic dispatch.

Had they written

   fn foo(bar: impl BarTrait)
the choice would've remained open to the caller

discuss

order

nicoburns|1 month ago

Right, but almost all APIs in Rust use something like

    fn foo(bar: impl BarTrait)
and AFAIK it isn't possible to write that in C (though C++ does allow this kind of thing).

bfrog|1 month ago

C++ you either use templates or classes and virtuals. In either case the caller doesn't get to decide.

bsaul|1 month ago

how do apis typically manage to actually « use » the « bar » of your example, such as storing it somewhere, without enforcing some kind of constraints ?