this post was submitted on 05 Jan 2025
60 points (90.5% liked)

Programming

17760 readers
499 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 18 hours ago (1 children)
[–] StrikeForceZero 1 points 16 hours ago* (last edited 16 hours ago) (1 children)

Yeah Interfaces would be the next best thing.

The only reason why traits are considered better is because in languages like rust it can enable static dispatch. Whereas interfaces in C#, Java, Typescript, (and C++ via abstract classes, not templates) are always dynamic dispatch.

[–] [email protected] 1 points 14 hours ago* (last edited 14 hours ago)

Looking at the Rust docs, it looks like it's not much more than a difference in implementation under the hood.

It would be clunky, but in C# you could duct tape this: make a static abstract method in an interface that takes an object named 'self', then an extension method that extends the class and just casts then runs the function with Unsafe.As<TFrom, TTo>(ref obj), or an explicitly aligned struct with overlapping values.

I don't expect any such implementation anytime soon though :/

ps: Typescript can go take a hike, it's a superset for a language that was never designed for this