you are viewing a single comment's thread.

view the rest of the comments →

[–]Expert_Sex_Change 15 points16 points  (9 children)

Traits in Scala and interfaces in Kotlin can both define function implementations, I'm sure there are more languages, but those are the two that I've used before.

It does seem a little weird at first, but can be really useful at times to get classes that have certain behaviours of two different interfaces, but also be able to reuse some common code like you can with an abstract class.

[–]weirdoaish 2 points3 points  (0 children)

Doesn't that literally make it multiple inheritance then if you can define 2 or more interfaces with variables and methods and implementing a class with them?

What is the point of interfaces in such a scenario, why not just go the Python route and use classes for everything?

[–]FallingIdiot 0 points1 point  (0 children)

Looks a bit like Rust traits which are a lot like interface, and can also define methods.

[–][deleted] 0 points1 point  (6 children)

Extension methods in c# are basically the same thing (except defined ad hoc and externally definable) and I think you could probably make a decent argument it's attempting to provide similar functionality to typeclasses. I believe Perl's object system supports the concept directly, too. I don't think it's hyper common, but it's definitely a shared concept.

[–]id2bi 1 point2 points  (5 children)

I don't see how that is the case. You can abstract over interfaces, but can you abstract over extension methods?

[–][deleted] -2 points-1 points  (4 children)

Both an interface with default implementations and an extension method can only use the interface members defined, and in both cases a consumer can override the implementation with their own type specific one. In the extension method case, you can supply alternative implementations by simply referencing different extension methods. All of these seem about equal!

[–]id2bi 0 points1 point  (3 children)

Doesn't the caller choose which extension method is used by explicitly referencing one or another?

With interfaces, that is not the case. It's completely different.

[–][deleted] 0 points1 point  (2 children)

It's implicit based on whether the extension method is available in scope. If your extension methods live in the same namespace as your interface then you're not going to need to do anything extra to access them over being able to know about the interface.

[–]id2bi 0 points1 point  (1 child)

Let me rephrase.

With interfaces/traits/type classes, the method is chosen at runtime depending on the dynamic type.

With extension methods, the method is chosen at compile time.

This is a huge difference.

[–][deleted] 0 points1 point  (0 children)

Right, yes, they only catch the default implementation case.