optional

How can we declare Optional Parameters in C#.net? [duplicate]

时间秒杀一切 提交于 2019-12-04 00:56:10
This question already has an answer here: Can I give a default value to parameters or optional parameters in C# functions? 6 answers I am using a method for doing some action, i want the method to be written only once by using Optional Parameters in C#, other than Method Overloading is there any? New to visual studio 2010 named and optional arguments for example public void ExampleMethod(int required, string optionalstr = "default string", int optionalint = 10) { } Have a look at following code Library to use using System.Runtime.InteropServices; Function declaration private void

Java 是如何优雅地处理NPE问题的

六月ゝ 毕业季﹏ 提交于 2019-12-04 00:48:55
1. 前言 对于 Java 开发者来说, null 是一个令人头疼的类型,一不小心就会发生 NPE (空指针) 问题。也是 Java 语言为人诟病的一个重要原因之一。在我们消除可恶的 NPE 问题之前我们要回顾一下 Java 中 null 的概念。 2. Java 中的 null 翻译自 Oracle Java 文档 Java语言中有两种类型,一种是 基本类型 ,另一种是 引用类型 。还有一种没有名字的特殊类型,即表达式 null 。 由于 null 类型没有名称,所以不可能声明为 null 类型的变量或者转换为 null 类型。 null 引用是 null 类型表达式唯一可能的值。 null 引用可以转换为任意引用类型。 事实上,程序员可以忽略 null 类型,可以认为 null 仅仅是一个可以成为任何引用类型的特殊符号。 从上面的描述我们可以了解到, 其实 null 仅仅是一个关键字标识量,既不是一种类型也不算对象,无法直接声明 null 和被转换为 null,仅仅只能被引用,null 可以转换为任何引用类型。当一个 Java 引用类型对象被引用为 null 时代表当前对象不引用对象,并没有为其分配内存。 这也是我们在没有引用的对象上调用方法出现空指针的根本原因。 大多数情况下 Java 开发者使用 null 是为了表示某种不存在的意思。 3. NPE 问题的解决

Optional Int in Realm

本小妞迷上赌 提交于 2019-12-04 00:03:03
I am trying to use an Optional Int in Realm and am getting an old error I think. Code dynamic var reps: Int? = nil Error 'Property cannot be marked dynamic because its type cannot be represented in Objective-C' I am using Realm 0.96.1 with XCode 7.1 I understand in the Realm documentation it says the Int isn't supported as an Optional but https://twitter.com/realm/status/656621989583548416 . That is from the Realm twitter so thats why I am confused. Are Optional Int supported or still no? From the Realm docs: String , NSDate , and NSData properties can be declared as optional or non-optional

Why does Maybe include Just?

放肆的年华 提交于 2019-12-03 23:19:17
问题 Thanks to some excellent answers here, I generally understand (clearly in a limited way) the purpose of Haskell's Maybe and that its definition is data Maybe a = Nothing | Just a however I'm not entity clear exactly why Just is a part of this definition. As near as I can tell, this is where Just itself is defined, but the the relevant documentation doesn't say much about it. Am I correct is thinking that the primary benefit of using Just in the definition of Maybe , rather than simply data

Stream.findFirst different than Optional.of?

倖福魔咒の 提交于 2019-12-03 22:24:05
Lets say I have two classes and two methods: class Scratch { private class A{} private class B extends A{} public Optional<A> getItems(List<String> items){ return items.stream() .map(s -> new B()) .findFirst(); } public Optional<A> getItems2(List<String> items){ return Optional.of( items.stream() .map(s -> new B()) .findFirst() .get() ); } } Why does getItems2 compile while getItems gives compiler error incompatible types: java.util.Optional<Scratch.B> cannot be converted to java.util.Optional<Scratch.A> So when I get the value of the Optional returned by findFirst and wrap it again with

How to peek on an Optional?

给你一囗甜甜゛ 提交于 2019-12-03 22:20:25
I want to use the fluent api of Optional and apply two Consumer s to it. I'm dreaming about something like this: Optional.ofNullable(key) .map(Person::get) .ifPresent(this::printName) .ifPresent(this::printAddress); // not compiling, because ifPresent is void How do I apply several Consumer s to an Optional ? Here's how you can implement the missing peek method for Optional : <T> UnaryOperator<T> peek(Consumer<T> c) { return x -> { c.accept(x); return x; }; } Usage: Optional.ofNullable(key) .map(Person::get) .map(peek(this::printName)) .map(peek(this::printAddress)); You can use this syntax:

Java 8 optional: ifPresent return object orElseThrow exception

别等时光非礼了梦想. 提交于 2019-12-03 22:13:22
I'm trying to make something like this: private String getStringIfObjectIsPresent(Optional<Object> object){ object.ifPresent(() ->{ String result = "result"; //some logic with result and return it return result; }).orElseThrow(MyCustomException::new); } This won't work, because ifPresent takes Consumer functional interface as parameter, which has void accept(T t). It cannot return any value. Is there any other way to do it ? Actually what you are searching is: Optional.map . Your code would then look like: object.map(o -> "result" /* or your function */) .orElseThrow(MyCustomException::new); I

Avoiding temporary when using boost::optional

一世执手 提交于 2019-12-03 18:03:39
问题 boost::optional support in_place construction like so: #include <boost/optional.hpp> #include <boost/utility/typed_in_place_factory.hpp> class Foo { int a,b; public: Foo(int one, int two) : a(one),b(two) {} }; int main() { boost::optional<Foo> fooOpt(boost::in_place<Foo>(1,3)); } Once we have an initialized fooOpt, is there a way of assigning a new Foo to it without creating a temporary? Something like : fooOpt = boost::in_place<Foo>(1,3); Thanks! 回答1: boost::optional #include <boost/optional

Subscript of a struct doesn't set values when created as an implicitly unwrapped optional

风流意气都作罢 提交于 2019-12-03 17:20:14
Why can't I change the the "numbers" array using subscripts when "Foo" is an implicitly unwrapped optional? struct Foo { var numbers = [0,0,0] subscript(index: Int) -> Int { get { return self.numbers[index] } set { self.numbers[index] = newValue } } } var fooA:Foo! fooA = Foo() fooA[1] = 1 // does not change numbers array fooA[1] // returns 0 fooA.numbers[1] = 1 // this works fooA[1] // returns 1 var fooB:Foo! fooB = Foo() fooB![1] = 1 // this works fooB![1] // returns 1 For some reason it works when I make "Foo" a class (called "Goo" below) class Goo { var numbers = [0,0,0] subscript(index:

How to know where Optional Chaining is breaking?

孤街浪徒 提交于 2019-12-03 13:43:44
So in iOS Swift we can do optional chaining to simplify the nil checking like in the official documentation let johnsAddress = Address() johnsAddress.buildingName = "The Larches" johnsAddress.street = "Laurel Street" john.residence!.address = johnsAddress if let johnsStreet = john.residence?.address?.street { println("John's street name is \(johnsStreet).") } else { println("Unable to retrieve the address.") } // prints "John's street name is Laurel Street." I understand about the usage of the optional chaining in john.residence?.address?.street but how can we know where is actually the chain