不可为 null 的对象有什么问题?

发布于 2024-07-11 23:38:07 字数 356 浏览 5 评论 0原文

我最近一直在研究 DbC 和 Spec#,它们似乎支持不可为 null 的对象。 不幸的是 Spec# 似乎已被放弃。

  1. Spec# 似乎内置了很多不错的语言功能,那么为什么它被放弃了呢?
  2. 默认情况下让所有对象不可为 null 会不会有任何问题,因此您必须编写 int?、string? 甚至邮件消息? 如果你真的想要一个可为空的对象?
  3. 我在这里看到了一种 Sql 类比 你可以在哪里查看课程 可空或不可空的属性 可以为空。 你甚至可以把 对属性的限制 可以使用sql表列吗?

我不认为在语言中内置这样的功能有什么问题。 有人可以启发我吗?

I have been looking at DbC lately and Spec# which seem to have support for non nullable objects. Unfortunately Spec# seem to have been abandoned.

  1. Spec# seemed to have lots of nice language features built in so why was it abandoned?
  2. Would there be any problem with letting all objects be non-nullable by default so you would have to write int?, string? and even MailMessage? if you really wanted a nullable object?
  3. I see kind of a Sql analogy here
    where you could check class
    properties as nullable or non
    nullable. Could you even put
    constraints on properties as you
    can with sql table columns?

I don't see the problems with having features like this built in to the language. Could anybody enlighten me on this?

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

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

发布评论

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

评论(1

能怎样 2024-07-18 23:38:07

您是否看到新的合同框架将成为.NET 4.0的一部分?

将其作为库而不是语言功能的好处是,它可以立即在所有语言中可用,而无需语言团队进行任何工作。 显然也有缺点......

链接:

说了这么多,我还希望能够写:

public Stream! Foo(string! x)

,表明 Foo 不得接收空引用,也不会返回空引用。 我认为,为这种类型的合约添加一些额外的语法会很方便。

Have you seen the new Contracts framework will will be part of .NET 4.0?

The benefit of making it a library rather than language feature is that it becomes immediately available in all languages, with no work on the part of the language teams. Obviously there are disadvantages too...

Links:

Having said all that, I'd like to be able to write:

public Stream! Foo(string! x)

as well, indicating that Foo must not receive a null reference, nor will it return one. Having an extra bit of syntax for just that type of contract would be handy, I think.

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