Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[proposal] Extensions Types or Traits #255

Open
3 of 5 tasks
ZverGuy opened this issue Jul 7, 2024 · 3 comments
Open
3 of 5 tasks

[proposal] Extensions Types or Traits #255

ZverGuy opened this issue Jul 7, 2024 · 3 comments
Assignees
Labels
api-specs Specification for std API language-specs request feature request from users

Comments

@ZverGuy
Copy link
Contributor

ZverGuy commented Jul 7, 2024

Extensions Types or Traits

  • Proposed
  • Triaged
  • Prototype: not started
  • Implementation: not started
  • Specification

Summary

Add support to extending class functionality without inheritance or composition

Motivation

Add support to extending class functionality without inheritance can decrease count of boilerplate code

With this feature creating adapters and wrapper for implement interfaces are useless

Just write a extension type or 'impl' for underlying type.

The purpose of "extensions" is to augment or adapt existing types to new scenarios, when those types are not under your control, or where changing them would negatively impact other uses of them. The adaptation can be in the form of adding new function members as well as implementing additional interfaces.

Example

Detailed design

Design based on https://github.com/dotnet/csharplang/blob/main/proposals/extensions.md

Syntax

type_declaration
    | extension_declaration // add
    | ...
    ;

extension_declaration
    : extension_modifier* ('implicit' | 'explicit') 'extension' identifier type_parameter_list? ('for' type)? type_parameter_constraints_clause* extension_body
    ;

extension_body
    : '{' extension_member_declaration* '}'
    ;

extension_member_declaration
    : constant_declaration
    | field_declaration
    | method_declaration
    | property_declaration
    | event_declaration
    | indexer_declaration
    | operator_declaration
    | type_declaration
    ;

extension_modifier
    | 'partial'
    | 'unsafe'
    | 'static'
    | 'protected'
    | 'internal'
    | 'private'
    | 'file'
    ;

but unlike c# proposal, in vein lang extension types should support to implement interfaces.

code example

class U
{
    public void M2() { }
}

explicit extension R : U
{
    public void M2() { } // warning: needs `new`
    void M()
    {
        M2(); // find `R.M2()`, no ambiguity
    }
}

Alternatives

Rust Traits

@ZverGuy ZverGuy added language-specs request feature request from users untriaged untriaged request labels Jul 7, 2024
@0xF6
Copy link
Member

0xF6 commented Jul 29, 2024

Okay
Create /specs/extensions.md and describe the specification
After pouring in the specification, work on the prototype will begin

@ZverGuy
Copy link
Contributor Author

ZverGuy commented Aug 2, 2024

Okay Create /specs/extensions.md and describe the specification After pouring in the specification, work on the prototype will begin

#259

@ZverGuy
Copy link
Contributor Author

ZverGuy commented Aug 2, 2024

Spec merged in upstream
@0xF6 for now I'm waiting prototype

@0xF6 0xF6 added api-specs Specification for std API and removed wait_topic_starter_action untriaged untriaged request labels Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-specs Specification for std API language-specs request feature request from users
Projects
None yet
Development

No branches or pull requests

2 participants