Nullable return types in PHP7

PHP 7 introduces return type declarations. Which means I can now indicate the return value is a certain class, interface, array, callable or one of the newly hintable scalar types, as is possible for function parameters. function returnHello(): string { return ‘hello’; } Often it happens that a value is not always present, and that … Read more

In Kotlin, what is the idiomatic way to deal with nullable values, referencing or converting them

If I have a nullable type Xyz?, I want to reference it or convert it to a non-nullable type Xyz. What is the idiomatic way of doing so in Kotlin? For example, this code is in error: val something: Xyz? = createPossiblyNullXyz() something.foo() // Error: “Only safe (?.) or non-null asserted (!!.) calls are allowed … Read more

nullable object must have a value

There is paradox in the exception description: Nullable object must have a value (?!) This is the problem: I have a DateTimeExtended class, that has { DateTime? MyDataTime; int? otherdata; } and a constructor DateTimeExtended(DateTimeExtended myNewDT) { this.MyDateTime = myNewDT.MyDateTime.Value; this.otherdata = myNewDT.otherdata; } running this code DateTimeExtended res = new DateTimeExtended(oldDTE); throws an InvalidOperationException … Read more

Nullable types and the ternary operator: why is `? 10 : null` forbidden? [duplicate]

This question already has answers here: Conditional operator assignment with Nullable<value> types? (6 answers) Why doesn’t this C# code compile? (4 answers) Closed 9 years ago. I just came across a weird error: private bool GetBoolValue() { //Do some logic and return true or false } Then, in another method, something like this: int? x … Read more

How can I format a nullable DateTime with ToString()?

How can I convert the nullable DateTime dt2 to a formatted string? DateTime dt = DateTime.Now; Console.WriteLine(dt.ToString(“yyyy-MM-dd hh:mm:ss”)); //works DateTime? dt2 = DateTime.Now; Console.WriteLine(dt2.ToString(“yyyy-MM-dd hh:mm:ss”)); //gives following error: no overload to method ToString takes one argument 21 Answers 21