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

Allow implementing methods without impl blocks #36

Open
osa1 opened this issue Jan 1, 2025 · 0 comments
Open

Allow implementing methods without impl blocks #36

osa1 opened this issue Jan 1, 2025 · 0 comments
Labels

Comments

@osa1
Copy link
Member

osa1 commented Jan 1, 2025

Consider the Str methods:

impl Str:
    fn fromUtf8Vec(bytes: Vec[U8]): Str = ...

    fn len(self): U32 = ...

    fn isEmpty(self): Bool = ...

    fn substr(self, byteStart: U32, byteEnd: U32): Str = ...

    fn startsWith(self, prefix: Str): Bool = ...

We should allow implementing these without an impl block:

fn Str.fromUtf8Vec(bytes: Vec[U8]): Str = ...

fn Str.len(self): U32 = ...

fn Str.isEmpty(self): Bool = ...

fn Str.substr(self, byteStart: U32, byteEnd: U32): Str = ...

fn Str.startsWith(self, prefix: Str): Bool = ...

Now sure how to best handle types with type parameters:

type Vec[T]: ...

impl[T] Vec[T]:
    fn withCapacity(cap: U32): Vec[T] = ...

Some options:

fn Vec[A].withCapacity(cap: U32): Vec[A] = ...  # A matches the type parameter in `type Vec[T]`

fn Vec.withCapacity(cap: U32): Vec[T] = ...     # T inherited from the type definition
@osa1 osa1 added the syntax label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant