-
Notifications
You must be signed in to change notification settings - Fork 214
Explicit operator #257
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
Comments
Dart does not have cast operators at all, so I'm not sure what you're asking to be able to mark explicit. |
@munificent but will there be an alternative so that we can simply convert one object into another? |
You can do this today. Just declare a method on your class that converts to the other class. class Foo {
Foo(this.value);
final int value;
Bar toBar() => Bar(value);
}
class Bar {
Bar(this.value);
final int value;
Foo toFoo() => Foo(value);
}
void main() {
Foo myFoo = Foo(1);
Bar myBar = myFoo.toBar();
print(myBar.value); // 1
} |
@Hixie thanks, but it would be nice to have syntax sugar for this. class Bar {
Bar(this.value);
final int value;
operator as(Foo foo) {
//extra logic here...
}
}
Bar myBar = myFoo;
var myBar = myFoo as Bar; |
Note that there is a related issue here: #107 , and in the other issue and pull request referenced from that issue. |
@listepo The sugar you suggest is longer than the current code... can you elaborate on what problem you want to solve? |
Yes, for example: |
Assuming that (In terms of types, A conversion from |
@Hixie I would like to avoid calling the method to convert. class Builder {
static run(Foo foo) {
print(foo);
}
}
void main() {
Builder.run(Bar(23).toFoo());
Builder.run(Bar(23)); // call as operator or alternative
}
|
@listepo wrote: ...
Builder.run(Bar(23)); // call as operator or alternative
... If you allow this kind of conversion to take place implicitly then we're basically talking about the same thing as Scala implicit conversions. For that, I'd like to mention that we have comments like the following:
It may seem very convenient to get things converted from any type to any type that we have taken the effort to declare an implicit conversion for, just because the type checker can say "ah, that Another example is that we have the subsection title 'Restricting implicit conversions' in the Medium article about 'What's new in Scala 3' (where it's mentioned that they will now enforce some changes that make it a bit less implicit what is going on). A third hint is that Martin Odersky singled out implicit conversions at his ECOOP/PLDI 2017 keynote as one of the few mechanisms that they actually don't really like any more. I'm not convinced that it will make programs more maintainable in the long run to have this kind of implicit conversions, and it seems that the Scala community has had similar considerations, after a while where they tried it out. |
Uh oh!
There was an error while loading. Please reload this page.
I would like to have something like this or alternative
The text was updated successfully, but these errors were encountered: