Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to initialize generic fields #973

Closed
deadsoul44 opened this issue Jan 26, 2021 · 6 comments
Closed

How to initialize generic fields #973

deadsoul44 opened this issue Jan 26, 2021 · 6 comments
Assignees

Comments

@deadsoul44
Copy link

Since type parameter cannot be accessed from static void _initializeBuilder method, it is not possible to initialize generic fields. Is there any workaround? I want to initialize based on all possible types of the generic. For example if T is int, initial value will be 0, if T is String, it will be ''.

@davidmorgan
Copy link
Collaborator

It should work to pass the builder to a generic method:

static void _initializeBuilder(FooBuilder b) => _initializeBuilderG(b);
static void _initializeBuilderG<T>(FooBuilder<T> b) {
  if (T == int) ...
}

it would make sense to have built_value support making _initializeBuilder itself generic, I'll treat this issue as a feature request.

Thanks.

@deadsoul44
Copy link
Author

I have to add type cast because analyzer is complaining about it.

  static void _initializeBuilder(FooBuilder b) => _initializeBuilderGeneric(b);
  static void _initializeBuilderGeneric<T>(FooBuilder<T> builder) {
    if (T == int) {
      builder
        ..field1 = (-1) as T?
        ..field2 = (-1) as T?;
    }
  }

@deadsoul44
Copy link
Author

This doesn't seem to work.

  static void _initializeBuilder(FooBuilder b) => _initializeBuilderGeneric(b);
  static void _initializeBuilderGeneric<T>(FooBuilder<T> builder) {
      builder
        ..field1 = ListBuilder<T>();
  }

I get this:

---------- ERROR ----------
Unsupported operation: explicit element type required, for example "new ListBuilder<int>"

------- STACK TRACE -------
#0      ListBuilder._checkGenericTypeParameter (package:built_collection/src/list/list_builder.dart:289:7)
#1      new ListBuilder._uninitialized (package:built_collection/src/list/list_builder.dart:267:5)
#2      new ListBuilder (package:built_collection/src/list/list_builder.dart:28:12)

@deadsoul44
Copy link
Author

The first example does not work either.

@davidmorgan
Copy link
Collaborator

Sorry, yes, that doesn't work. You'll need to use 'is', like this

  static void _initializeBuilder(InitializeGenericValueBuilder b) {
    if (b is InitializeGenericValueBuilder<int>) {
      b.value = 3;
    }
  }

actually it looks like it does work to get the generic type parameter, although I did not design this :) e.g.

  static void _initializeBuilder<T>(InitializeGenericValueBuilder<T> b) {
    if (T == int) {
      b.value = 3 as T;
    }
  }

you still to use is somehow to avoid casting to T, though, so it doesn't actually get you much.

I'll add some test coverage for this.

@davidmorgan
Copy link
Collaborator

3d971e4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants