避免在DART中复制无效检查

发布于 2025-02-02 15:58:44 字数 1050 浏览 1 评论 0原文

我目前的目标是删除此代码重复:

final int? myNullableInt = 10;

/// Everywhere I need to do this null verification:
if (myNullableInt == null) return null;

return someOtherMethodThatReceivesANonNullableInt(myNullableInt);

我想将其转换为Kotlin中的内容:

final int? myNullableInt = 10;

return myNullableInt?.apply((myInt) => someOtherMethodThatReceivesANonNullableInt(myInt));

我做到了:

extension ApplyIfNotNull on Object? {
  T? apply<T extends Object?>(Object? obj, T? Function(Object) fn) {
    if (obj == null) return null;
    return fn(obj);
  }
}

但这给了我静态错误:

The argument type 'Object' can't be assigned to the parameter type 'int'.

注意:这应该与所有类型一起使用,例如int s,字符串 s,doublemyownClasStype s。

我能做什么?还是我想念什么?

My current goal is to remove this code duplication:

final int? myNullableInt = 10;

/// Everywhere I need to do this null verification:
if (myNullableInt == null) return null;

return someOtherMethodThatReceivesANonNullableInt(myNullableInt);

I want to convert to something like we have in Kotlin:

final int? myNullableInt = 10;

return myNullableInt?.apply((myInt) => someOtherMethodThatReceivesANonNullableInt(myInt));

I did it:

extension ApplyIfNotNull on Object? {
  T? apply<T extends Object?>(Object? obj, T? Function(Object) fn) {
    if (obj == null) return null;
    return fn(obj);
  }
}

But this gives me a static error:

The argument type 'Object' can't be assigned to the parameter type 'int'.

Note: this should work with all types, e.g ints, Strings, double and MyOwnClassTypes.

Is there something I can do? or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2025-02-09 15:58:44
 扩展对象上的扩展名applifnotnull? {
  t?应用&lt; t扩展对象?&gt;(对象?obj,t?function(object)fn){
    if(obj == null)返回null;
    返回fn(obj);
  }
}
 

这是行不通的,因为它声明回调能够接受任何对象参数,但是您可能会尝试将其与仅接受int的函数一起使用。争论。还不清楚为什么您制作了扩展方法,因为它根本不涉及接收器(this)。

您还需要在回调的参数类型上使您的函数通用:(

R? applyIfNotNull<R, T>(T? obj, R Function(T) f) =>
    (obj == null) ? null : f(obj);

这与我在 https://github.com/dart-lang/lange/langage/issues/360#issuecomment-502423488 ,但随着参数的反转。)

或作为扩展方法,以便它可以在上使用此,而不是拥有额外的obj参数:

extension ApplyIfNotNull<T> on T? {
  R? apply<R>(R Function(T) f) {
    // Local variable to allow automatic type promotion.  Also see:
    // <https://github.com/dart-lang/language/issues/1397>
    final self = this;
    return (self == null) ? null : f(self);
  }
}

另请参见 https://github.com/dart-lang/lange/sissues/360 对于现有的语言功能请求,以及此时其他建议的解决方法。

extension ApplyIfNotNull on Object? {
  T? apply<T extends Object?>(Object? obj, T? Function(Object) fn) {
    if (obj == null) return null;
    return fn(obj);
  }
}

That doesn't work because it declares that the callback be capable of accepting any Object argument, but you're presumably trying to use it with a function that accepts only an int argument. It's also unclear why you've made an extension method since it doesn't involve the receiver (this) at all.

You need to make your function generic on the callback's argument type as well:

R? applyIfNotNull<R, T>(T? obj, R Function(T) f) =>
    (obj == null) ? null : f(obj);

(That's the same as what I suggested in https://github.com/dart-lang/language/issues/360#issuecomment-502423488 but with the arguments reversed.)

Or, as an extension method, so that it can work on this instead of having the extra obj argument:

extension ApplyIfNotNull<T> on T? {
  R? apply<R>(R Function(T) f) {
    // Local variable to allow automatic type promotion.  Also see:
    // <https://github.com/dart-lang/language/issues/1397>
    final self = this;
    return (self == null) ? null : f(self);
  }
}

Also see https://github.com/dart-lang/language/issues/360 for the existing language feature request and for some other suggested workarounds in the meantime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文