Skip to content

"typedef" with reference name. #4309

Open
@srburton

Description

@srburton

Hello everyone, a good son returns home! 😊

I contributed to the bool.parse feature, and now I'm interested in contributing a new feature for typedef.

I’d like to gather community feedback on whether it makes sense for typedef, which serves as an alias for a function, to also carry a refname. This would allow scenarios like the following example to work:

Current Code

import 'dart:ui';

import 'package:flutter/widgets.dart';

typedef OnSetup = VoidCallback;
typedef OnClearName = VoidCallback;
typedef OnBtnClick = VoidCallback;

class People {

  static final Map<Type, List<Function>> _callbacks = {};

  /// Register a callback for the specific type
  static void on<A extends Function>(A callback) {
    _callbacks.putIfAbsent(A, () => []).add(callback);
  }

  /// Execute the callback registered of the specific type
  static void call<A>() {
    if (_callbacks.containsKey(A)) {
      for (var callback in _callbacks[A]!) {
        callback();
      }
    }
  }
}

main(){
    People .on<OnSetup>((){
       print('Hi');
    });

    People .on<OnClearName>((){
       print('Dart');
    });

    People .on<OnBtnClick >((){
        print('How are you?');
     });

    People.call<OnSetup>();
}

Output

Hi
Dart
How are you?

The expected

import 'dart:ui';

import 'package:flutter/widgets.dart';

typedef OnSetup = VoidCallback;
typedef OnClearName = VoidCallback;
typedef OnBtnClick = VoidCallback;

class People {

  static final Map<String, List<Function>> _callbacks = {};

  /// Register a callback for the specific type
  static void on<A>(A callback) {
    _callbacks.putIfAbsent(A.refname, () => []).add(callback);
  }

  /// Execute the callback registered of the specific type
  static void call<A.refname>() {
    if (_callbacks.containsKey(A.refname)) {
      for (var callback in _callbacks[A.refname]!) {
        callback();
      }
    }
  }
}

main(){
    People .on<OnSetup>((){
       print('Hi');
    });

    People .on<OnClearName>((){
       print('Dart');
    });

    People .on<OnBtnClick >((){
        print('How are you?');
     });

    People.call<OnSetup>();
}

Output

Hi

Justification

My proposal is to introduce a refname property to typedef, allowing comparisons based on the alias name rather than just the function type. This would make it easier to work with type-based mappings, such as event-driven architectures or dependency injection scenarios.

Impact

N/A

Mitigation

N/A

Change Timeline

N/A

Associated CLs

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions