Maybe, but it depends on the language and the type of val. The posted code differs from yours in a language where == performs a non-strict comparison, but returning something that doesn't match the return type doesn't cause a coercion. I don't know whether such a language exists.
You can also do return val ?? false because of the null coalescing operator, but it isn't common. Basically the ?? operator returns the first (leftmost) non-null operand. And it evaluates lazily. So you can in theory chain them like this:
`return foo ?? bar ?? baz ?? throw new ArgumentNullException();
Even though this syntax is convenient it isn't used very much.
The method does not return an exception, the expression is just lazily evaluated. e.g. if foo is not null it just returns foo and does not evaluate the rest of the expression. If it reaches the throw the method does not return anything, it just throws.
If you mean what is the base type of ArgumentNullException, all exceptions are derived fromSystem.Exception
The expression foo ?? bar ?? baz ?? throw new ArgumentNullException() must surely have a type substitutable for boolean, since that's the expected return type. I assume a ?? expression has the type that's common to both operands.
I don't know a lot about C#, but for example in Scala, which is a similar language in many ways, a throw expression has the type Nothing, which is a subtype of Boolean (and also of every other type), which is why an expression like that would compile.
13
u/MCWizardYT Jun 23 '20
this depends on the code using isTrue though.