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

Trying to get this type is incredibly unweildy. Ref: Self from Rust #4098

Open
TekExplorer opened this issue Sep 22, 2024 · 1 comment
Open
Labels
request Requests to resolve a particular developer problem

Comments

@TekExplorer
Copy link

Sometimes, I want to get "this" type. This is often side-stepped by the use of covarient in;
for example; didUpdateWidget(covarient ThisWidget oldWidget)

Additionally, I find myself occasionally wanting to do something like:

abstract class Foo<Self extends Foo<Self>> {}

class Bar extends Foo<Bar> {}

as a way to extract the "this" type, but it can get really messy really quickly.

One possible usecase is to have a copyWith interface as such:

interface class CopyWith {
  Self copyWith();
}

class Foo {
  Self copyWith() => Foo();
} 

class Bar extends Foo { // Self changed, so Foo.copyWith is treated as abstract
  Self copyWith() => Bar();
}

To add on, this could have strong synergy with static interfaces, as we could then operate on Self such as doing:

static interface CreateDefault {
  factory create();
}
base mixin CopyWith on CreateDefault {
  Self copyWith() => Self.create();
}

Granted, copyWith is likely not the strongest example for this, but its 2:30 am and I don't have an example on hand, on account of being forced to give up :/

I want to see what ideas others have with regards to the usefulness of a Self-like "type"

@TekExplorer TekExplorer added the request Requests to resolve a particular developer problem label Sep 22, 2024
@lrhn
Copy link
Member

lrhn commented Sep 22, 2024

Avoiding F-bounded generics would be nice. It probably won't be enough with Self for that if you have mutually dependent types, like a built value and a builder.

The Self type has problems with inheritance. It effectively means that you can't just inherit the superclass method, and must override the method in each subclass. Even if you don't want to. If you have a Self returning method in an interface, you can't have a skeleton method for it that a private implementation class can use. You can't have multiple private implementation classes that return another version than themselves.
This is not a hypothetical issue.
If num had a self-typed unary minus, the VM's integer implementation wouldn't work, because -2^62 is implemented by a Smi and 2^62 is not.

There would have to be a way to opt out of the self-typing for private implementation classes, but I can't see a good way to do that. (And being a "private class" isn't enough, it had to be some kind of secret class that cannot be leaked at all. Otherwise things would break if the library used the private class as a type argument, or had a public alias for it.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants