Coder Social home page Coder Social logo

Nested typedefs about language HOT 15 OPEN

dart-lang avatar dart-lang commented on May 24, 2024 5
Nested typedefs

from language.

Comments (15)

eernstg avatar eernstg commented on May 24, 2024 3

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 typedefs seem relevant as well, because they are both about abbreviation of unwieldy type expressions.

from language.

gbracha avatar gbracha commented on May 24, 2024 1

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.

eseidelGoogle avatar eseidelGoogle commented on May 24, 2024 1

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.

zoechi avatar zoechi commented on May 24, 2024 1

I find inline function types make code hard to read. I prefer typedefs.

from language.

kasperl avatar kasperl commented on May 24, 2024

Added Area-Language, Triaged labels.

from language.

bwilkerson avatar bwilkerson commented on May 24, 2024

I'm not sure, but issue dart-lang/sdk#27527 might be related to your need.

from language.

fsc8000 avatar fsc8000 commented on May 24, 2024

@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.

munificent avatar munificent commented on May 24, 2024

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.

lrhn avatar lrhn commented on May 24, 2024

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.

mit-mit avatar mit-mit commented on May 24, 2024

Completed via #65

from language.

mraleph avatar mraleph commented on May 24, 2024

@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.

mit-mit avatar mit-mit commented on May 24, 2024

This issue is about allowing typedefs to be define within classes

Got it, thanks

from language.

mit-mit avatar mit-mit commented on May 24, 2024

Related issue for nested classes: #336

from language.

lrhn avatar lrhn commented on May 24, 2024

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.

lrhn avatar lrhn commented on May 24, 2024

... 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.