标签: non-nullable

Eclipse null分析:int类型的表达式需要未经检查的转换以符合'@Nonnull Integer'

配置Eclipse 4.2.0以执行空分析(配置为使用@javax.annotation.Nonnull等)时,以下代码将生成警告

空类型安全:int类型的表达式需要未经检查的转换以符合'@Nonnull Integer'

class C
{
  static void foo(int i)
  {
    bar(i); // Warning
  }

  static void bar(@javax.annotation.Nonnull Integer i)
  {
  }
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题(不使用@SuppressWarnings("null"))?似乎分析器不知道盒装基元不可能null.

java eclipse null static-analysis non-nullable

6
推荐指数
1
解决办法
1252
查看次数

我们能否确保`+(nonnull instancetype)sharedInstance的可空性;`?

这是一个如何优雅地规避课堂上的可空性的init问题NSObject.

所以这是一个经典的objective-c实现:

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    static id sharedInstance;
    dispatch_once(&onceToken, ^
    {
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
Run Code Online (Sandbox Code Playgroud)

但是现在我想声明它nonnull,如果可能的话:

+ (nonnull instancetype)sharedInstance;
Run Code Online (Sandbox Code Playgroud)

不幸的是,init返回一个nullable instancetype值.我应该NSAssert在打电话后添加一个什么东西init

我注意到有些人甚至将价值观记录nonnull在现实中nullable.那有意义吗?

我应该大胆地简单地添加到NS_ASSUME_NONNULL_BEGIN任何地方,而不是真正确保价值观nonnull吗?

nullable objective-c init non-nullable objective-c-nullability

6
推荐指数
1
解决办法
1623
查看次数

Kotlin - 可空场的非空吸气剂

我是Kotlin的新手,我尝试将一个小型Java项目改造成这种新语言.我在我的项目中使用mongodb,我有一个类,例如:

class PlayerEntity {

  constructor() {} //for mongodb to create an instance

  constructor(id: ObjectId, name: String) { //used in code
    this.id = id
    this.name = name
  }

  @org.mongodb.morphia.annotations.Id
  var id: ObjectId? = null

  var name: String? = null
}
Run Code Online (Sandbox Code Playgroud)

由于空构造函数,我必须将id字段标记为nullable(var id: ObjectId?).当我尝试从另一个类访问此字段时,我必须使用非null检查:thePlayer.id!!.但我的应用程序的逻辑是该id字段永远不为null(mongo创建一个Player实例并立即设置id字段).而且我不想在任何地方进行非空检查.

我试图创建一个非null的getter,但它不能编译:

var id: ObjectId? = null
    get(): ObjectId = id!!
Run Code Online (Sandbox Code Playgroud)

我也可以为id创建一些存根,并在构造函数中使用它,但这看起来像一个肮脏的黑客:

val DUMMY_ID = new ObjectId("000000000000000000000000");
Run Code Online (Sandbox Code Playgroud)

那么有解决问题的解决方法吗?

java getter nullpointerexception non-nullable kotlin

6
推荐指数
1
解决办法
1642
查看次数

C#8 什么是“默认!” 对泛型类型做什么?

只是尝试使用 C# 8.0 Beta 并更新一些代码以使用可为 null 的引用类型。

我有一个用于 Trie 实现的节点样式类。每个节点都有一个类型为 的值T。根节点的构造函数不需要值,因此我将其设置为default

这是一个简短的版本:

public class Trie<T>
{
    public readonly bool caseSensitive;
    public readonly char? letter;
    public readonly Dictionary<char, Trie<T>> children;
    public readonly Trie<T>? parent;
    public readonly int depth;
    public bool completesString;
    public T value;

    public Trie(bool caseSensitive = false)
    {
        this.letter = null;
        this.depth = 0;
        this.parent = null;
        this.children = new Dictionary<char, Trie<T>>();
        this.completesString = false;
        this.caseSensitive = caseSensitive;
        this.value = default;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果 ctor …

c# generics non-nullable c#-8.0 nullable-reference-types

6
推荐指数
2
解决办法
5189
查看次数

打字稿:问号与未定义的类型联合

当在名称末尾指定带有问号的成员时,类型签名会自动扩展为 include undefined。创建一个没有这个成员的实例是可以的:

interface Option{
    val? : number; // hover over val and it tells you that the type is number|undefined
}

let o: Option = {};
Run Code Online (Sandbox Code Playgroud)

推断出的 val 类型是number|undefined。到目前为止,我认为这是问号的唯一效果。但是手动注释类型联合并没有相同的效果:

interface Union{
    val : number|undefined;
}

let u: Union = {}; // compiler complains about missing member val
Run Code Online (Sandbox Code Playgroud)

为什么第二个代码示例不正确?

non-nullable typescript

6
推荐指数
1
解决办法
376
查看次数

如何注释数组NonNull?

我正在使用org.eclipse.jdt.annotation.NonNull为静态空值分析添加额外信息.我不知道如何正确地注释数组:

  1. 我怎么能说数组引用是非空的?
  2. 我怎么能说数组由非null元素组成?

我测试过:

    public static void test(@NonNull String[] a) {
        assert a != null;
    }

    public static void main(String[] args) {
        test(null);
    }
Run Code Online (Sandbox Code Playgroud)

但是,Eclipse没有标记test(null);为错误.

java eclipse non-nullable

5
推荐指数
2
解决办法
1236
查看次数

为什么不允许在FQN上使用@NonNull注释?

它是Eclipse中的错误还是为什么我不能用完全限定类型名称(FQN)注释参数@NonNull

    import org.eclipse.jdt.annotation.NonNull;

    public class Foo {
        // No problem
        public void bar(@NonNull String x) {
        }

        // Error: Type annotations are not allowed on type names used to access
        // static members
        public void baz(@NonNull java.lang.String x) {
        }
    }
Run Code Online (Sandbox Code Playgroud)

java eclipse non-nullable

5
推荐指数
1
解决办法
570
查看次数

C#8中非空引用类型的默认值是什么?

如果启用了可为空的引用类型,那么如果这样声明,则以下字符串的值是什么?

string text;
Run Code Online (Sandbox Code Playgroud)

c# non-nullable c#-8.0 nullable-reference-types

5
推荐指数
1
解决办法
405
查看次数

如何在类中初始化不可为空的字符串?

我有这个项目:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="3.0.1" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

这是主要的类:

namespace CSharp8
{
    using System;
    using Microsoft.Extensions.Configuration;

    internal class Program
    {
        private static void Main()
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", false, true)
                .Build();
            var appSettings = configuration.Get<AppSettings>();
        }

        public class AppSettings
        {
            public string SourceServer { get; set; }
            public string TargetServer { get; set; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是appsettings.json:

{
  "SourceServer": "prodtools",
  "TargetServer": "qatools"
}
Run Code Online (Sandbox Code Playgroud)

在 AppSettings 的属性上,我收到此警告:

警告 CS8618 不可为 …

c# string initialization non-nullable

5
推荐指数
1
解决办法
2248
查看次数

“退出构造函数时,不可为空的事件必须包含非空值”

我收到警告“退出构造函数时,不可为空的事件‘SomeEvent’必须包含非空值。请考虑将事件声明为可空。”

这是我的代码的一个非常简化的版本,它复制了完全相同的问题。我在这里缺少什么?这和.Net 6 有什么关系吗?

namespace ConsoleApp3
{
    public delegate void SomeDelegate(object sender, EventArgs args);

    public class NewClass
    {
        public NewClass(string name)
        {
            this.name = name;

        }

        public string name { get; set; }

        public event SomeDelegate SomeEvent;
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# events delegates non-nullable

5
推荐指数
1
解决办法
6039
查看次数