optional

Using Color As Optional Parameter In a function within a class

回眸只為那壹抹淺笑 提交于 2019-12-19 05:48:01
问题 How can I declare an optional color parameter in some function or sub, as if I do that in the normal way (I mean to give some default color for that optional parameter) as the vb.net compiler complains that there is some error in that code. How do I resolve this issue. Sample code below: Public Shared Function SomeFunction(ByVal iParam As Integer, Optional ByVal oColor As Color = Color.Black) End Function The compiler does not accept '=Color.Black' 回答1: MSDN says about Optional Parameters for

Optional binding of nil literal vs. variable that is nil in Swift

↘锁芯ラ 提交于 2019-12-19 04:38:10
问题 In Swift, why does var x: Int? = nil if let y: Int? = x { ... } behave differently from if let y: Int? = nil { ... } My understanding of why the first case succeeds suggests that the second should as well, so I must not really be understanding. The latter is not failing because of an invalid assignment, nor because of optional chaining; and otherwise it seems the same as the former. Why does the latter fail, and how is it different from the former. Exactly at what point, and for what reason,

Is it okay to “Move” an object from a queue, if you're about to pop from it?

谁说我不能喝 提交于 2019-12-18 18:53:58
问题 I've been working on a parser for commands (which are fancy wrappers around large arrays of data), and have a queue that unhandled commands reside on. If I need a command, I query it with code like this: boost::optional<command> get_command() { if (!has_command()) return boost::optional<command>(nullptr); else { boost::optional<command> comm(command_feed.front()); //command_feed is declared as a std::queue<command> command_feed.pop(); return comm; } } The problem is, these commands could be

Regex optional group

戏子无情 提交于 2019-12-18 18:38:22
问题 I am using this regex: ((?:[a-z][a-z]+))_(\d+)_((?:[a-z][a-z]+)\d+)_(\d{13}) to match strings like this: SH_6208069141055_BC000388_20110412101855 separating into 4 groups: SH 6208069141055 BC000388 20110412101855 Question: How do I make the first group optional, so that the resulting group is a empty string? I want to get 4 groups in every case, when possible. Input string for this case: (no underline after the first group) 6208069141055_BC000388_20110412101855 回答1: You can easily simplify

Java示例演示Functor 和monad

偶尔善良 提交于 2019-12-18 16:59:27
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> This article was initially an appendix in our Reactive Programming with RxJava book. However introduction to monads, albeit very much related to reactive programming, didn't suit very well. So I decided to take it out and publish separately as a blog post. I am aware that " my very own, half correct and half complete explanation of monads " is the new " Hello, world " on programming blogs. Yet the article looks at functors and monads from a specific angle of Java data structures and libraries. Thus I thought it's worthwhile sharing. RxJava was designed and

How to encode Int as an optional using NSCoding

谁说胖子不能爱 提交于 2019-12-18 14:56:36
问题 I am trying to declare two properties as optionals in a custom class - a String and an Int. I'm doing this in MyClass: var myString: String? var myInt: Int? I can decode them ok as follows: required init?(coder aDecoder: NSCoder) { myString = aDecoder.decodeObjectForKey("MyString") as? String myInt = aDecoder.decodeIntegerForKey("MyInt") } But encoding them gives an error on the Int line: func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeInteger(myInt, forKey: "MyInt") aCoder.encodeObject

Why does moving std::optional not reset state

一笑奈何 提交于 2019-12-18 14:06:08
问题 I was rather surprised to learn that the move constructor (and assignment for that matter) of std::optional does not reset the optional moved from, as can be seen in [19.6.3.1/7] which states "bool(rhs) is unchanged." This can also be seen by the following code: #include <ios> #include <iostream> #include <optional> #include <utility> int main() { std::optional<int> foo{ 0 }; std::optional<int> bar{ std::move(foo) }; std::cout << std::boolalpha << foo.has_value() << '\n' // true << bar.has

Optional in Lombok

只愿长相守 提交于 2019-12-18 13:53:16
问题 I have a class called Address which looks like this: @Value class Address { @NotNull String userId; @NotNull String line1; String line2; private Address(Builder b) { // copy everything from builder } // override getter for line2 so that it returns Optional<String> public Optional<String> getLine2() { return Optional.ofNullable(this.line2); } // and a Builder public static class Builder { // builder methods } } Here I am forced to write Builder and a Getter because, if I want to return an

How are optional values implemented in Swift?

眉间皱痕 提交于 2019-12-18 12:13:09
问题 I wonder how the value types in Swift (Int, Float...) are implemented to support optional binding ("?"). I assume those value types are not allocated on the heap, but on the stack. So, do they rely on some kind of pointer to the stack that may be null, or does the underlying struct contain a boolean flag ? 回答1: Optionals are implemented as enum type in Swift. See Apple's Swift Tour for an example of how this is done: enum OptionalValue<T> { case None case Some(T) } 回答2: Swift is open source

Should I use Java8/Guava Optional for every method that may return null?

点点圈 提交于 2019-12-18 10:08:51
问题 Optional is used to represent nullable object, Some uses of this class include As a method return type, as an alternative to returning null to indicate that no value was available To distinguish between "unknown" (for example, not present in a map) and "known to have no value" (present in the map, with value Optional.absent()) To wrap nullable references for storage in a collection that does not support null (though there are several other approaches to this that should be considered first)