Skip to content

Implement special itself type #32

@joeldrapper

Description

@joeldrapper

Since types are evaluated at the class level, the type self will typically refer to the class that the method is defined on. For example here the self return value refers to Example.

class Example
  fun foo => self do
    self
  end
end

This works, but the signature could be more precise. For example, if the function returns another Example object instead of returning itself, it would still be valid.

class Example
  fun foo => self do
    Example.new
  end
end

That’s because self here means Example. It’s equivalent to

class Example
  fun foo => Example do
    Example.new
  end
end

But what if we want to type a function that actually returns itself? There should be a way to do that.

I think we should introduce itself as a special type like never and void. itself refers to the self that the method is bound to. With itself, this would be valid:

class Example
  fun foo => itself do
    self
  end
end

But this would not:

class Example
  fun foo => itself do
    Example.new
  end
end

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions