shaedrich,
@shaedrich@mastodon.online avatar

I've been #programming for 14 years now, have been using #PHP, #JavaScript, #ColdFusion, #Ruby, and whatnot, but holy cow, when reading the following chapter, I've literally been yelling "what the heck" at every second paragraph:

https://tutorial.ponylang.io/types/traits-and-interfaces

I mean, #PonyLang really tries to explain everything in depth, and I appreciate the effort, but while it works fine in earlier chapters, it confuses the heck out of me in this at length.

#amProgramming #amCoding #computerScience

Crell,
@Crell@phpc.social avatar

@shaedrich That all sounds pretty neat to me. It's kind of a hybrid of Go and Rust. Which part is confusing to you?

shaedrich,
@shaedrich@mastodon.online avatar

Reader after first reading the chapter: "Interfaces and traits are the same"
:

Crell,
@Crell@phpc.social avatar

@shaedrich Pony traits sound like PHP traits, but if you could "instanceof" against them as well.

Structural interfaces sound lkek they're straight out of Go. Conformance is implicit.

Both have implications for "instanceof", but go about it differently.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell That doesn't contradict what you're replying to.

As you said with PHP, in PHP instances and traits work more visibly different.

Crell,
@Crell@phpc.social avatar

@shaedrich I guess I'm having a hard time understanding the complex part. It's two related features that overlap, but in a way that gives you more flexibility. Pony is far from the only language that does that.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell They essentially overlap 95 %—so why both?

Crell,
@Crell@phpc.social avatar

@shaedrich Why do both abstract classes and traits exist in PHP? Because they do importantly different things, even if they also overlap.

Interface is "how can I use this from the outside".
Trait is "how can I assemble this from parts", but you can also get a type flag out of it.

Crell,
@Crell@phpc.social avatar

@shaedrich It sounds from the OP that you've worked mainly in weak-type languages, which probably explains why it feels alien. In compiled, type-centric languages there's usually a ton of features to statically guarantee that everything will work at runtime. Interfaces, generics, traits (in various forms), interfaces with default methods, etc. Those are very common in type-centric languages.

I've worked mainly in typed PHP and dabbled in Go, Rust, Kotlin. So these things are self-evident for me

shaedrich,
@shaedrich@mastodon.online avatar

@Crell I have my fair share with typed PHP, so this per se isn't alien to me.

Crell,
@Crell@phpc.social avatar

@shaedrich So is it that you don't understand the features, or is it a "but... why?" response?

(Note: I hope I'm not coming off as talking down to you; that's not my intent. I really am just trying to educate, and understand the hesitation because I've been pushing for stronger type support in PHP for some time, and understanding the arguments against helps.)

shaedrich,
@shaedrich@mastodon.online avatar

@Crell Yeah, "importantly different"

Interfaces in PHP are added after the class name just like in Pony, but traits are used in the class body, making these two distinct, which doesn't happen in Pony

Crell,
@Crell@phpc.social avatar

@shaedrich Because PHP traits don't interact with the type system. (Though people often argue they should.) Pony traits do.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell And why do they?

Crell,
@Crell@phpc.social avatar

@shaedrich I cannot speak for the Pony devs. I would suspect for the same reasons people give for why they should in PHP. Basically, it fills the niche that abstract classes do, but more cleanly. So, yes, it becomes multiple inheritance. (Something most typed languages have now, in fact. It's less scary than C++ made it out to be.)

Crell,
@Crell@phpc.social avatar

@shaedrich I've had PHP code like this:

interface I { public function foo(); }

trait DefaultI { public function foo() { ... }

class Thing implements I {
use DefaultI;
}

Now do ~3 of those in the same class. If use DefaultI implied implements I (basically Pony), that makes the code a lot easier to write and understand.

class A implements BehaviorA, BehaviorB, BehaviorC {}

I would have use for that personally.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell I'm afraid, I don't exactly get your point here 🤔

Crell,
@Crell@phpc.social avatar

@shaedrich Imagine instead it was:

trait I { public function foo(); }

class Thing implements I { }

That's what Pony traits let you do, essentially. (At least from the doc; I've never used it.)

Whereas interfaces are more like how Go works, where "if it has the methods of a duck, it's a duck."

They have to be separate, as otherwise every class would conform to every trait and implicitly inherit its implementations. Which is just a mess.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell I guess, I'm a lost cause 😅

Crell,
@Crell@phpc.social avatar

@shaedrich 😛 Which part did you not follow?

shaedrich,
@shaedrich@mastodon.online avatar

@Crell I'm still at the "but… why?" question. Maybe, let's switch positions.

I know what traits are for in PHP, and I know what interfaces are for in PHP. I know how to use traits in PHP, and I know, how to use interfaces in PHP. I know their differences and similarities. So far, so good.

This system might not be perfect, but it makes sense to me. What I don't understand is, making these terms almost interchangeable in Pony, aside from open-world enumerations and private methods.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell I see, Pony traits and interfaces come from different languages. Nothing wrong with that per se. But these languages seem to have different concepts for interfaces and traits, and combining these different concepts into one language results in a strange mix.

Crell,
@Crell@phpc.social avatar

@shaedrich (Well, I'm on team "add default interface methods to PHP", which would mostly eliminate the need for traits in PHP: https://wiki.php.net/rfc/interface-default-methods)

The key difference is structural typing. Interfaces are implicit in Pony.

Imagine if in PHP, this just worked:

trait T {
public function foo(): string { print "foo"; }
}

class C {}
$c = new C();

$c->foo();

That would be a mess.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell Currently, "default methods" can be achieved with abstract classes, when one doesn't want to use both interfaces and traits, while Pony doesn't offer inheritance at all.

Crell,
@Crell@phpc.social avatar

@shaedrich The problem with abstract classes is you can have only one of them. A trait or interface is multi-use.

Have a look at this file, and the other (very small) files in the same directory. This would be much cleaner if interfaces had default methods, or traits implied implements. Abstract classes wouldn't help here.

shaedrich,
@shaedrich@mastodon.online avatar

@Crell Well, that's probably because someone decided, they don't like polymorphism ^^

Crell,
@Crell@phpc.social avatar

@shaedrich LOL. Who, me, the PHP team in 2004, or the Pony team? 😛

I've written on the difference between "is a" and "makes use of" before, when advocating for traits. Though I think default methods are a better answer most of the time:

https://www.garfieldtech.com/blog/beyond-abstract

  • All
  • Subscribed
  • Moderated
  • Favorites
  • programming
  • ngwrru68w68
  • rosin
  • GTA5RPClips
  • osvaldo12
  • love
  • Youngstown
  • slotface
  • khanakhh
  • everett
  • kavyap
  • mdbf
  • DreamBathrooms
  • thenastyranch
  • magazineikmin
  • megavids
  • InstantRegret
  • normalnudes
  • tacticalgear
  • cubers
  • ethstaker
  • modclub
  • cisconetworking
  • Durango
  • anitta
  • Leos
  • tester
  • provamag3
  • JUstTest
  • All magazines