Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to typedef a generic function?

Tags:

dart

dart-2

I have a generic static method that looks like this:

static build<K>() {
    return (GenericClass<K> param) => MyClass<K>(param);
}

So far I have tried:

typedef F = MyClass<K> Function(GenericClass<K> param);

but it says that:

The return type '(GenericClass<K>) → MyClass<K>' isn't a '(GenericClass<dynamic>) → MyClass<dynamic>', as defined by the method 'build'.

and

typedef F = SimpleViewModel<K> Function<k>(Store<K> param);

Which says that:

The return type '(GenericClass<K>) → MyClass<K>' isn't a '<K>(GenericClass<K>) → MyClass<K>', as defined by the method 'build'.

MyClass looks like this:

class MyClass<T> {
  final GenericClass<T> param;

  MyClass(this.param);


  static build<K>() {
      return (GenericClass<K> param) => MyClass<K>(param);
  }
}

So, what is a valid typedef for it?

like image 578
arielnmz Avatar asked Jun 28 '18 22:06

arielnmz


People also ask

How do you define a generic function?

Generic functions are true functions that can be passed as arguments, returned as values, used as the first argument to funcall and apply, and otherwise used in all the ways an ordinary function may be used.

What is generic function give example?

Generics is the idea to allow type (Integer, String, … etc and user-defined types) to be a parameter to methods, classes and interfaces. For example, classes like an array, map, etc, which can be used using generics very efficiently. We can use them for any type.

What is generic function in C?

Generic functions are functions declared with one or more generic type parameters. They may be methods in a class or struct , or standalone functions. A single generic declaration implicitly declares a family of functions that differ only in the substitution of a different actual type for the generic type parameter.

How do I use typedef to flutter?

With the help of typedef, we can also assign a variable to a function. Syntax:typedef variable_name = function_name; After assigning the variable, if we have to invoke it then we go as: Syntax: variable_name( parameters );


1 Answers

There are two concepts of "generic" when it comes to a typedef. The typedef can be generic on a type, or the typedef can refer to a generic function (or both):

A typedef which is generic on T:

typedef F<T> = T Function(T);

Then in usage:

F first = (dynamic arg) => arg; // F means F<dynamic>
F<String> second = (String arg) => arg; // F<String> means both T must be String

A typedef which refers to a generic function on M:

typedef F = M Function<M>(M);

Then in usage:

F first = <M>(M arg) => arg; // The anonymous function is defined as generic
// F<String> -> Illegal, F has no generic argument
// F second = (String arg) => arg -> Illegal, the anonymous function is not generic

Or both:

typedef F<T> = M Function<M>(T,M);

And in usage:

F first = <M>(dynamic arg1, M arg2) => arg2; // F means F<dynamic> so the T must be dynamic
F<String second = <M>(String arg1, M arg2) => arg2; // The T must be String, the function must still be generic on the second arg and return type
like image 172
Nate Bosch Avatar answered Oct 10 '22 22:10

Nate Bosch