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

meta limitation: useResult not available in factory constructors #55962

Closed
bwc-x opened this issue Jun 10, 2024 · 4 comments
Closed

meta limitation: useResult not available in factory constructors #55962

bwc-x opened this issue Jun 10, 2024 · 4 comments
Assignees
Labels
analyzer-pkg-meta Issues related to package:meta area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@bwc-x
Copy link

bwc-x commented Jun 10, 2024

Can't seem to find any existing open/closed issue regarding this.

Currently facing a situation, roughly akin to:

import 'package:meta/meta.dart';

extension Ext on Class {
  void doSomeWork(int i) {
    print('$this + $i = ...');
    return this;
  }
}

class Class {
  const Class();
  const factory Class.sub() = SubClass;
}

class SubClass extends Class {
  const SubClass();
}

void main() {
  Class.sub(); // .doSomeWork(5) meant to be called, yet skipped on accident
}

Given a new instance, built using a factory - sometimes containing a great deal of inner parameters, passed to it - it is impossible to check whether the result is actually used. The slightly more practical (actual) use case for this involves passing a rather complex instance, built through a redirecting const factory into a Flutter's BuildContext for subsequent state propagation / widget tree update. Assigning it to a temporary variable beforehand, though necessary at times, is completely redundant in the rest of cases.

The @useResult annotation would be nothing short of perfect for this. Yet it vehemently refuses to do its job, insisting that The annotation 'useResult' can only be used on fields, getters, methods, top-level functions, or top-level variables. While ignoring that message makes it ignore all the uses of the resulting instance completely.

image

Any way to patch this up?

A rather messy fix for this currently involves either prior redundant assignment, or an even more redundant static method declaration - reduplicating the whole factory constructor in whole merely for a chance to annotate it with @useResult.

class Class {
  const Class();
  @useResult // no errors
  static Class sub() => SubClass();
}

Wouldn't mind doing this either, if it wasn't for the freezed 's own reliance on factory constructors for its code generation.

@lrhn lrhn added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jun 12, 2024
@srawlins srawlins added P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug analyzer-pkg-meta Issues related to package:meta labels Jun 12, 2024
@srawlins
Copy link
Member

I don't see why you shouldn't be able to annotate any kind of constructor with @useResult. The docs don't include it currently. https://pub.dev/documentation/meta/latest/meta/useResult-constant.html

@bwilkerson @pq any opinions?

@pq
Copy link
Member

pq commented Jun 12, 2024

I don't see why you shouldn't be able to annotate any kind of constructor with @useResult.

I agree. I expect it was just an oversight.

@bwilkerson
Copy link
Member

I also agree. I would think that allowing it on constructors should be well defined and reasonable.

@srawlins srawlins self-assigned this Jun 12, 2024
@srawlins
Copy link
Member

Short memory. I did this two months ago. You just need the next Dart version. :D Maybe not released yet. eb7ba49

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-pkg-meta Issues related to package:meta area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants