nullable

Can we ensure nullability of `+ (nonnull instancetype)sharedInstance;`?

流过昼夜 提交于 2019-12-07 03:34:03
问题 This is a question on how to gracefully circumvent the nullability of init in NSObject class. So here is a classic objective-c implementation: + (instancetype)sharedInstance { static dispatch_once_t onceToken; static id sharedInstance; dispatch_once(&onceToken, ^ { sharedInstance = [[self alloc] init]; }); return sharedInstance; } But now I want to declare it as nonnull , if possible: + (nonnull instancetype)sharedInstance; Unfortunately, init returns a nullable instancetype value. Should I

Nullable properties vs. Nullable local variables

此生再无相见时 提交于 2019-12-07 03:20:57
问题 I am puzzled by the following behavior of Nullable types: class TestClass { public int? value = 0; } TestClass test = new TestClass(); Now, Nullable.GetUnderlyingType(test.value) returns the underlying Nullable type, which is int . However, if I try to obtain the field type like this FieldInfo field = typeof(TestClass).GetFields(BindingFlags.Instance | BindingFlags.Public)[0]; and I invoke Nullable.GetUnderlyingType(field.FieldType).ToString() it returns a System.Nullable[System.Int32] type.

C-like language without NULL?

一笑奈何 提交于 2019-12-07 02:26:43
问题 Hi I was recently watching an old video about how null pointers were a billion dollar mistake. He points out both C# and java as they have run-time checks but don't completely eliminate it enough and this is somewhat understandable. He also points out the C at one point which he feels so sure of is a great problem. I get that null terminated strings, arrays with no length and a few other things are bad (billions of dollars on buffer overflow exploits) but to completely remove null? Some

Scala: Something like Option (Some, None) but with three states: Some, None, Unknown

只愿长相守 提交于 2019-12-07 02:05:54
问题 I need to return values, and when someone asks for a value, tell them one of three things: Here is the value There is no value We have no information on this value (unknown) case 2 is subtly different than case 3. Example: val radio = car.radioType we know the value: return the radio type, say "pioneer" b. there is no value: return None c. we are missing data about this car, we don't know if it has a radio or not I thought I might extend scala's None and create an Unknown, but that doesn't

Moq cannot create mock object of class with nullable parameter in the constructor

回眸只為那壹抹淺笑 提交于 2019-12-07 01:00:54
问题 I have this class: public class TestClass { public TestClass(int? foo, string bar) { //..Something } } I am trying to mock it using MOQ like this var mockA = new Mock<A>(new object[] {(int?)1, string.Empty}) or this, var mockA = new Mock<A>(new object[] {1 as int?, string.Empty}) even this var mockA = new Mock<A>(new object[] {(int?)null, string.Empty}) When I try to get the object like this var objA = mockA.Object it throws this exception Can not instantiate proxy of class: TestClass. Could

What is the size of a Nullable<Int32>?

橙三吉。 提交于 2019-12-06 19:28:58
问题 So, a couple of questions, actually: An int ( Int32 ) is specified to be (obviously) 32 bits. What about an int? ( Nullable<int> )? My gut tells me that it would be 32 bits for the integer plus 8 more bits for the boolean, but perhaps the implementation is more intricate than that. I would have answered my own question using sizeof(int?) ; but as int? is a managed type this is not allowed. I understand that the size of a type may be platform-dependent, and that in the case of objects which

C# determine a Nullable property DateTime type when using reflection

核能气质少年 提交于 2019-12-06 19:03:01
问题 I have a question on how to determine an object's Nullable property type. ObjectA has a property DateTime? CreateDate; When I iterate through its properties like the following code, how do I check if a property is a Nullable DateTime type? foreach (PropertyInfo pi in ObjectA.GetType().GetProperties()) { //do the compare here } 回答1: pi.PropertyType == typeof(DateTime?) 回答2: pi.PropertyType == typeof(Nullable<DateTime>); 回答3: Try: property.PropertyType.Equals(typeof(DateTime?)) 来源: https:/

How to use Objective-C __nonnull in a backwards-compatible way?

非 Y 不嫁゛ 提交于 2019-12-06 04:17:01
问题 Xcode has recently added __nonnull , __nullable , etc. attributes. However, they're not supported by older versions of clang and other compilers. How can I use these attributes in a compatible way? I hoped something like this would work: #ifndef NS_ASSUME_NONNULL_BEGIN #define __nonnull #endif but it seems that NS_ASSUME_NONNULL_BEGIN is not a real macro, and it's "not defined" in Xcode7. And it would make sense for this to work: #if !defined(__is_identifier) || __is_identifier(__nonnull)

Using nullable types in Linq expressions

二次信任 提交于 2019-12-05 19:15:21
var quantSubset = from userAns in userAnalysis.AllUserAnswers join ques in userAnalysis.AllSeenQuestions on userAns.QID equals ques.QID where (ques.QuestionType == "QT") select new { QuestionLevel = ques.LevelID, TimeTaken = userAns.TimeTaken, Points = userAns.Points, UsedWeapon = (userAns.UsedBy2 && userAns.UsedHint), WasCorrect = userAns.WasCorrect.HasValue ? userAns.WasCorrect.Value : null }; In my select expression I want to select a nullable type WasCorrect (last part of the expression) but apparently I cannot do it the way I am currently trying. How can I get WasCorrect as nullable type

C# - Good way to expose Nullable<T> to COM

拈花ヽ惹草 提交于 2019-12-05 18:07:50
问题 We are working on exposing an assembly to COM. Among other things, we frequency use nullable values such as long?, DateTime?, etc. These are generic types and can't be exposed to COM. What is a good substitute for these data types for COM? We have tried the following: //Original CustomerID property in class public long? CustomerID { get; set; } //Explicit COM interface long IComInterface.CustomerID { get { return CustomerID.GetValueOrDefault(); } set { CustomerID = value; } } The problem is,