Why getter_not_subtype_setter_types?

110 views
Skip to first unread message

Natalie Weizenbaum

unread,
Dec 7, 2020, 8:39:12 PM12/7/20
to General Dart Discussion
Hi folks,

As part of our preparation for NNBD, I ran into an issue where I declared a getter as nullable (because it could be called before underlying data was initialized) and the corresponding setter as taking a non-nullable argument (because it sets the underlying data, and we don't want it to be able to be set to the uninitialized value). To my surprised, this caused an analysis error saying that the getter's return type had to be a subtype of the setter's argument type.

Can you give some context around why this requirement is in place? It's forcing me to write a runtime check for something that it seems like I should be able to express in the type system.

Thanks!
- Natalie

Erik Ernst

unread,
Dec 8, 2020, 6:07:16 AM12/8/20
to Dart Misc
There's no problem expressing it in the type system, or checking it during compilation, etc.

But the rationale for this rule (which has been around in some form since the language specification was first added to the source code control system in Oct 2013) has always been that a getter/setter pair is a conceptual unit, and hence the getter and setter should have somewhat similar signatures. They are not intended to be used as completely unrelated entities, they should "set" or "get" something that makes sense as a "property". Currently this is enforced by saying that the getter return type must be a subtype of the setter parameter type.

One location where this discussion has been taken recently is https://github.com/dart-lang/language/issues/331.

Thanks!
- Natalie

--
For more ways to connect visit https://dart.dev/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/CAKDS1k6jz8vBweVzfZtOfvBybWYSgu%3DNnHpCOCBPn3%3DD2%2BtUTg%40mail.gmail.com.


--
Erik Ernst  -  Google Danmark ApS
Skt Petri Passage 5, 2 sal, 1165 København K, Denmark
CVR no. 28866984

Reply all
Reply to author
Forward
0 new messages