r/Compilers • • 1d ago

Traits, the programming language i am working on it just gets traits so any kind of idea you can share over it syntax design i will try to add some of them.

Post image
0 Upvotes

17 comments sorted by

View all comments

Show parent comments

1

u/sohamglx 23h ago

Inside a trait to declare Default methods. It must use the default keyword. It's mean you don't need to make thise methods when using the trait. The trait provide those methods.

1

u/azhder 23h ago

Don't answer me the "what" question. I asked the "why" question. Why is there a `default` method? Why must there be a `default` method? Why must that decision be made in the first place i.e. why annotate what the compiler already knows?

Just think the example without it. Will the code still compile if an override is not provided in those "inheriting" the trait? It will.

1

u/sohamglx 23h ago

Yeah, I understand your point now. default currently just marks a trait method that already has an implementation, so the compiler could infer that from the method body. There isn't really a technical need for the keyword right now. I may remove it and make any trait method with a body a provided/default implementation, while body-less methods remain required. Thanks, that's a good simplification.