We have traits at home

(axelmagn.com)

17 points | by Ygg2 10 hours ago

3 comments

  • Panzerschrek 1 hour ago
    I see little to no benefits of introducing such "traits" into a language having duck-typing in templates. It may sometimes improve code readability (slightly), but with more boilerplate code for traits handling in exchange. And the result machine code will be identical to code with no traits at all (with only duck-typing).

    C++ is a good example of this. It had duck-typing in templates for decades. Then some sort of trait-like mechanism named concepts has been introduced. The only benefit of them seems to be slightly better error messages if someone misuses a template class/function.

    • tialaramex 17 minutes ago
      I'm not a Zig expert but it looks as though the intent is that duck-typing is not a core part of this idea. Having traits allows Carol to implement Ben's Teleport trait for Alice's Goose.

      With duck-typing Carol needs to either prevail upon Alice to make you a teleporting Goose, or make her own "CarolsTeleportingGoose" wrapper type which implements the teleporting functions.

    • indiosmo 51 minutes ago
      Concepts are great for overload sets where previously you needed enable_if gymnastics. Also useful for if constexpr.

      If you do template metaprogramming it makes your life a lot easier.

  • no1guangming 1 hour ago
    Can't load the page
  • ZeroDayDreamer 36 minutes ago
    [flagged]