nullable

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

让人想犯罪 __ 提交于 2019-12-05 07:38:32
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 seem possible. suggestions? thanks! Update: Ideally I'd like to be able to write code like this: car

Nullable properties vs. Nullable local variables

我与影子孤独终老i 提交于 2019-12-05 06:04:45
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. So that means the method Nullable.GetUnderlyingType() has a different behavior depending on how you

nullable type and a ReSharper warning

寵の児 提交于 2019-12-05 05:20:51
I have the following code: private static LogLevel? _logLevel = null; public static LogLevel LogLevel { get { if (!_logLevel.HasValue) { _logLevel = readLogLevelFromFile(); } return _logLevel.Value; } } private static LogLevel readLogLevelFromFile() { ... } I get a ReSharper warning on the return statement about a possible System.InvalidOperationException and it suggests I check _logLevel to see if it is null first. However, readLogLevelFromFile returns LogLevel , not LogLevel? , so there is no way the return statement could be reached when _logLevel is null . Is this just an oversight by

REST with nullable types?

我与影子孤独终老i 提交于 2019-12-05 04:01:12
I've hit a brickwall. My REST implementation won't accept Nullable values. [OperationContract] [WebInvoke(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/Transactions?AccNo={AccNo}&CostCentreNo={CostCentreNo}&TransactionType={TransactionType}&Outstanding={Outstanding}&CheckStartDate={CheckStartDate}&CheckEndDate={CheckEndDate}")] List<Transactions> GetTransactions(Int32 AccNo, Int32 CostCentreNo, Int32 TransactionType, Boolean Outstanding, DateTime? CheckStartDate, DateTime? CheckEndDate); Whereas my original SOAP implementation does. So is there a way around this? Or do I have to re

@Nullable and SonarQube 'Conditionally executed blocks should be reachable' warning

♀尐吖头ヾ 提交于 2019-12-05 03:51:33
Package has following package-info.java: @ParametersAreNonnullByDefault package foo; import javax.annotation.ParametersAreNonnullByDefault; Class has the following method: private static String toIsoString(@Nullable Instant dateTime) { return dateTime == null ? null : dateTime.toString(); } On which SonarQube (Version 6.2, SonarJava 4.14.0.11784) gives the following warning (squid:S2583): How can I convince SonarQube that the code is actually correct? Interestingly, SonarLint plugin (3.0.0.2041) in Idea doesn't generate the same warning. Apparently, this problem was caused by us using sonar

The '?' character cannot be used here

无人久伴 提交于 2019-12-05 02:37:22
With these variables: Dim d1 As Date? = Nothing Dim d2 As DateTime? = Nothing Dim i1 As Integer? = Nothing Dim i2 As Int32? = Nothing Why am I allowed to do this?: Dim flag1 As Boolean = Date?.Equals(d1, d2) Dim flag2 As Boolean = Integer?.Equals(i1, i2) ...but not allowed to do this?: Dim flag3 As Boolean = DateTime?.Equals(d2, d1) Dim flag4 As Boolean = Int32?.Equals(i2, i1) The last code will fail with an error saying: The '?' character cannot be used here. VB.NET developers are not supposed to be using C# keywords (religion, you know). Seriously though, I agree with @Konrad this looks like

Convert to a Nullable<T> from a string using Reflection

浪尽此生 提交于 2019-12-05 02:19:11
问题 How can I convert TO a Nullable from a String using reflection? I have the following code to convert TO almost any value type given almost any value. There is quite a bit of code above this to use the IsAssignableFrom, etc. so this is the last resort. MethodInfo parse = t.GetMethod("Parse", new Type[] { typeof(string) }); if (parse != null) { object parsed = parse.Invoke(null, new object[] { value.ToString() }); return (T)parsed; } else { throw new InvalidOperationException("The value you

Shielding nullable domain properties with ViewModel

喜夏-厌秋 提交于 2019-12-05 02:16:53
问题 I am using Entity Framework 4.0, and making use of POCO objects. When I populate POCO objects from the DB, I translate property values to my own Domain objects, which we can call my Model. Necessarily, whether or not the fields of my Model are Nullable depends on whether the value it maps to in the database comes from a NULL or NOT NULL column. I won't go into detail, but the values must be nullable in the DB, because a user can partially save a draft of the object before publishing it to the

WCF - convert empty element to nullable native type

做~自己de王妃 提交于 2019-12-05 01:38:14
问题 Leaving a SOAP field element empty results in a cast error for native types. (sadly cannot use xsi:nil="true" due to client constraints) Marking the WCF contract native type as nullable<> does not appear to be enough to stop the following error being returned to the client. The string '' is not a valid Boolean value. at System.Xml.XmlConvert.ToBoolean(String s) at System.Xml.XmlConverter.ToBoolean(String value) System.FormatException does anyone know the best method of instructing the

Extension method for nullable enum

浪子不回头ぞ 提交于 2019-12-05 01:35:10
I'm trying to write an Extension method for nullable Enums. Like with this example: // ItemType is an enum ItemType? item; ... item.GetDescription(); So I wrote this method which doesn't compile for some reason that I don't understand: public static string GetDescription(this Enum? theEnum) { if (theEnum == null) return string.Empty; return GetDescriptionAttribute(theEnum); } I'm getting the following error on Enum? : only non-nullable value type could be underlying of system.nullable Why? Enum can not have the value null ! Update: If have lots of enums, ItemType is just an example of one of