non-nullable

Alternatives to nullable types in C#

安稳与你 提交于 2019-12-03 05:55:50
I am writing algorithms that work on series of numeric data, where sometimes, a value in the series needs to be null. However, because this application is performance critical, I have avoided the use of nullable types. I have perf tested the algorithms to specifically compare the performance of using nullable types vs non-nullable types, and in the best case scenario nullable types are 2x slower, but often far worse. The data type most often used is double, and currently the chosen alternative to null is double.NaN. However I understand this is not the exact intended usage for the NaN value,

What would we do without NULL?

被刻印的时光 ゝ 提交于 2019-12-02 19:08:17
I once read that having nullable types is an absolute evil. I believe it was in an article written by the very person who created them(in Ada?) I believe this is the article Anyway, so what if by default a language like C# used non-nullable types? How would you replace some of the common idioms in C# or Ruby or any other common language where null is an acceptable value? Instead of outright declaring that nullable types are evil, I would posit: most languages graft nullability onto entire kinds of types, when the two concepts should really be orthogonal . For example, all non-primitive Java

Type Inference failed in a call to 'join' on nullable and non-nullable int

♀尐吖头ヾ 提交于 2019-12-01 00:17:24
问题 In my Linq, I am trying to make an inner join to a nullable field. Employee and Department have a relation, Department may have an EmployeeID or may have a null. So what would be my join, if i want only the records that satisifed the inner join (no result for null EmployeeIDs): var result = from emp in employees join dept in departments on new { Source = emp.EmployeeID } equals new { Source = dept.EmployeeID }; I am getting an exception: The type of one of the expressions in the join clause

Why is null not allowed for DateTime in C#?

只谈情不闲聊 提交于 2019-11-30 10:45:51
Why it is not allowed to assign null to a DateTime in C#? How has this been implemented? And can this feature be used to make your own classes non-nullable? Example: string stringTest = null; // Okay DateTime dateTimeTest = null; // Compile error I know that I can use DateTime? in C# 2.0 to allow null to be assigned to dateTimeTest and that I could use Jon Skeet's NonNullable class on my string to get a run time error on the assignment of stringTest. I'm just wondering why the two types behave differently. DateTime is a value-type ( struct ), where-as string is a reference-type ( class etc).

About the non-nullable types debate

橙三吉。 提交于 2019-11-29 14:01:27
I keep hearing people talk about how non-nullable reference types would solve so many bugs and make programming so much easier. Even the creator of null calls it his billion dollar mistake , and Spec# has introduced non-nullable types to combat this problem. EDIT: Ignore my comment about Spec#. I misunderstood how it works. EDIT 2: I must be talking to the wrong people, I was really hoping for somebody to argue with :-) So I would guess, being in the minority, that I'm wrong, but I can't understand why this debate has any merit. I see null as a bug-finding tool. Consider the following: class

Right way to use the @NonNull annotation in Android Studio

蓝咒 提交于 2019-11-29 11:25:13
问题 I'd like to use the @NonNull annotation in Android, but I can't figure out just the right way to do it. I propose you this example: public void doStuff(@NonNull String s){ //do work with s... } So when i call doStuff(null) the IDE will give me a warning. The problem is that I cannot rely on this annotation since, like this question points out, they don't propagate very far. So I'd like to put a null check on my method, like this: if(s==null) throw new IllegalAgrumentException(); But the IDE,

IntelliJ IDEA @ParametersAreNonnullByDefault for all subpackages

五迷三道 提交于 2019-11-29 10:59:55
I use IntelliJ's null-checking mechanism to prevent nullpointer crashes. I've successfully set up all Java method parameters to be @NonNull by default using this answer. After creating package-info.java which is used to define package annotations in Java. All java files which are direct descendants of that package have methods with default @NonNull parameters in my Android Studio project. The obvious problem is that I need to define @ParametersAreNonnullByDefault for all Java classes in that package i.e. including all subpackages . How can I define @ParametersAreNonnullByDefault so that it

Value of unassigned non-nullable variable (C#)

风流意气都作罢 提交于 2019-11-29 06:58:22
Just curious. If you go: string myString; Its value is null. But if you go: int myInt; What is the value of this variable in C#? Thanks David Firstly, note that this is only applicable for fields, not local variables - those can't be read until they've been assigned, at least within C#. In fact the CLR initializes stack frames to 0 if you have an appropriate flag set - which I believe it is by default. It's rarely observable though - you have to go through some grotty hacks. The default value of int is 0 - and for any type, it's essentially the value represented by a bit pattern full of zeroes

Non-nullable reference types

末鹿安然 提交于 2019-11-29 06:09:12
I'm designing a language, and I'm wondering if it's reasonable to make reference types non-nullable by default, and use "?" for nullable value and reference types. Are there any problems with this? What would you do about this: class Foo { Bar? b; Bar b2; Foo() { b.DoSomething(); //valid, but will cause exception b2.DoSomething(); //? } } My current language design philosophy is that nullability should be something a programmer is forced to ask for, not given by default on reference types (in this, I agree with Tony Hoare - Google for his recent QCon talk). On this specific example, with the

Non-nullable reference types

主宰稳场 提交于 2019-11-29 06:07:28
I'm designing a language, and I'm wondering if it's reasonable to make reference types non-nullable by default, and use "?" for nullable value and reference types. Are there any problems with this? What would you do about this: class Foo { Bar? b; Bar b2; Foo() { b.DoSomething(); //valid, but will cause exception b2.DoSomething(); //? } } My current language design philosophy is that nullability should be something a programmer is forced to ask for, not given by default on reference types (in this, I agree with Tony Hoare - Google for his recent QCon talk). On this specific example, with the