Comments (15)
The other use case for local typedefs that we have discussed involves type variables:
class Thing<A, B, C> {
typedef Factory = Thing<A, B, C> Function({A a, B b, List<C> cs = []});
// `Factory` used many times.
}
A global typedef
could be used instead, but it would require writing something like ThingFactory<A, B, C>
everywhere inside class Thing
(and renaming Factory
to ThingFactory
seems reasonable, because we are adding a name to the library scope, i.e., to a much more widely seen namespace).
It's definitely possible to argue that this abbreviation is a good thing or that it is a bad thing, but if we decide to support allowing all types (not just function types) on the right hand side of a global typedef
then local typedef
s seem relevant as well, because they are both about abbreviation of unwieldy type expressions.
from language.
Our need for typedefs stems from issues around the function type syntax. We can't support anonymous function types very well, which leads to these problems. One can work around them with generic typedefs though.
typedef void Callback<S> (S s);
class Foo<T> {
Callback<T> _callback;
Foo(this._callback);
...
}
Set owner to @gbracha.
Added Accepted label.
from language.
Presumably this is still an issue, but not actively being worked on. :)
I came across this looking for an issue on how to make a typedef for a specialized generic, like Map<String, Map<string, dynamic>
, etc (which I believe is currently impossible in Dart, but likely to be a more common request in strong mode). Presumably that's not this bug?
e.g. https://dartpad.dartlang.org/31438e9a6ac9d097c3b6642f66abf935
from language.
I find inline function types make code hard to read. I prefer typedefs.
from language.
Added Area-Language, Triaged labels.
from language.
I'm not sure, but issue dart-lang/sdk#27527 might be related to your need.
from language.
@eseidelGoogle Yes, currently typedef
is limited to function types. I agree, there seems no reason why it should not work for arbitrary type aliases. And there is actually an old issue for this: dart-lang/sdk#2626
from language.
This is still an issue, but is a different one from what you describe. This one is about allowing typedef
to occur inside a class body.
from language.
I still see no reason to prohibit local typedefs, but the new function type syntax allows you to write function types directly so you don't need a typedef to write a function type.
from language.
Completed via #65
from language.
@mit-mit I think this is incorrect. This issue is about allowing typedefs to be define within classes - I don't think we have ever done this. The corresponding language issue is #67
from language.
This issue is about allowing typedefs to be define within classes
Got it, thanks
from language.
Related issue for nested classes: #336
from language.
Nested typedefs are slightly simpler than nested classes because typedefs won't introduce a new namespace level.
Even if we end up choosing to not allow nested classes, I'd still be interested in nested typedefs (or, heck, local typedefs declared inside functions).
from language.
... and with generalized type aliases, the above comment is no longer correct.
We allow accessing static members through a type alias for a class type (as long as the type alias precisely that class type),
which means that allowing publicly accessible nested type aliases will effectively grant nested namespaces:
abstract final class _NameSpace2 {
static final int x = 42;
}
abstract final class ns1 {
static final int x = 37;
typedef ns2 = _NameSpace2;
}
void main() {
print(ns1.x);
print(ns1.ns2.x); // Works?
}
On way to avoid that is to make "local" type alias declaration not become part of the external scope.
Like (but opposite) constructors that are only accessible as ClassName.name
, not just name
through the lexical scope,
a typedef
declaration inside another declaration would only be accessible through the lexical scope, not as OuterScope.aliasName
. It's a local declaration, not a publicly visible one.
Another approach would be to allow declarations to nest. You'd have to write static
on all the nested declarations of a class
, to show that they're part of its namespace, not its instance content, but we could also introduce a plain namespace
declaration where all members are implicitly "static". (It's the "zero, one or an infinite amount of any feature" issue, and we're currently at "1" namespace depth. Not counting import prefixes.).
from language.
Related Issues (20)
- Unbounded implementation specific behavior. HOT 7
- Cross-library private access grants HOT 5
- Smart cast not smart enough. HOT 2
- Nested object pattern HOT 1
- Add using keyword for resource management HOT 2
- Allow destructuring into class fields HOT 2
- Dynamic `is` / inheritance inspection HOT 7
- abstract interface - implicit vs explicit getter/setter HOT 5
- Inferred record types + optional fields for record expressions? HOT 5
- Records as an argument of lambda expression HOT 2
- Allow subclassing a sealed class outside the library with an abstract class HOT 31
- Let record types have a constructor. HOT 2
- Add `isAbc` getters for enums per enum value HOT 2
- [inline-classes] Allow enhanced usage of type variables? HOT 8
- Non-virtual and re-declared virtual members. HOT 3
- Extension methods need to be able to call method of same name in base class HOT 2
- `case` doesn't work with nullable types HOT 12
- Inconsistent explanations between `identical()` and `Expando` HOT 5
- Function literal return type inference fails on divergent future HOT 11
- Implicit parameters to help reduce boilerplate HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from language.