optional

Use of Optional in a map

一笑奈何 提交于 2019-12-04 06:20:33
Ok before I start explaining my question I want you to know that I know about the design idea behind Optional and that it isn't intended to be used in fields or collections, but I have programmed a lot in Kotlin currently and really dislike using null . So I have a Node based editor like in the Unreal Engine and each node has ConnectionBox es, which can be either free or are occupied by a Connection . So there are different ways to express this one of which is using a map that maps each ConnectionBox to a Connection like: Map<ConnectionBox, Connection> connectionEndPoints; and Connection could

How to convert Option[Try[_]] to Try[Option[_]]?

喜欢而已 提交于 2019-12-04 06:16:56
I quite often use the function below to convert Option[Try[_]] to Try[Option[_]] but it feels wrong. Can be such a functionality expressed in more idiomatic way? def swap[T](optTry: Option[Try[T]]): Try[Option[T]] = { optTry match { case Some(Success(t)) => Success(Some(t)) case Some(Failure(e)) => Failure(e) case None => Success(None) } } Say I have two values: val v1: Int = ??? val v2: Option[Int] = ??? I want to make an operation op (which can fail) on these values and pass that to function f below. def op(x: Int): Try[String] def f(x: String, y: Option[String]): Unit I typically use for

How to optionally add a comma and whitespace to a capture group?

别等时光非礼了梦想. 提交于 2019-12-04 05:07:07
问题 I am trying to match five substrings in each block of text (there are 100 blocks total). I am matching 99% of the blocks of text, but with a few errors regarding groups 3 and 4. Here is a demo link: https://regex101.com/r/cW2Is3/4 Group 3 is "parts of speech", and group 4 is an English translation. In the first block of text, det, pro should all be in group 3, and then the; him, her, it, them should be in group 4. The same issue occurs again in the third block of text. Group 3 should be adj,

std::optional and polymorphism

旧时模样 提交于 2019-12-04 05:03:58
I have read on Stack Overflow in many posts that when pointer is used (for argument or return value) and nullptr is allowed (making it optional), it is generally better to use std::optional instead. However what if the pointer refers to polymorphic type? Is it best to use std::optional or a pointer? optional doesn't work with polymorphic types. It's a value type, and polymorphic base classes don't work in an optional . Just as putting polymorphic base classes in vector or similar containers doesn't work. Return a pointer. There's a reason why the advice is usually stated as "generally". The

How to upcast object contained in Java 8 Optional?

耗尽温柔 提交于 2019-12-04 04:21:36
Is there an efficient way to perform upcasting while using an Optional object. Here is a sample code: class A{} class B extends A{} B func(){ //do something return new B(); } Optional<B> func2(){ //do something return Optional.of(new B()); } main() { A a = func(); // Upcasting works fine B b = func(); // Upcasting works fine Optional<B> b = func2(); // 1. Works fine Optional<A> a = func2(); // 2. How to make this work ? } (2.) gives an error. I can solve it by creating another function. But is there an efficient way, so that func2() can be used for both (1.) and (2.) ? I would write a method

Python argparse : how to detect duplicated optional argument?

感情迁移 提交于 2019-12-04 03:43:57
问题 I'm using argparse with optional parameter, but I want to avoid having something like this : script.py -a 1 -b -a 2 Here we have twice the optional parameter 'a', and only the second parameter is returned. I want either to get both values or get an error message. How should I define the argument ? [Edit] This is the code: import argparse parser = argparse.ArgumentParser() parser.add_argument('-a', dest='alpha', action='store', nargs='?') parser.add_argument('-b', dest='beta', action='store',

Does NSNumberFormatter.stringFromNumber ever return nil?

淺唱寂寞╮ 提交于 2019-12-04 03:09:02
问题 It seems to me that any valid number can also be expressed as a String , so I don't know why this function returns a String? instead of a String . 回答1: My best guess would be because of the legacy support. This is from the official documentation: The behavior of an NSNumberFormatter object can conform either to the range of behaviors existing prior to OS X v10.4 or to the range of behavior since that release. NSNumberFormatter Class Reference 来源: https://stackoverflow.com/questions/28103113

Real world examples of @optional protocol methods

你离开我真会死。 提交于 2019-12-04 03:08:27
问题 I'm learning Objective-C at the moment and have come across optional methods in Protocols. My background is C# and can see a Protocol as something similar to a C# Interface. Where a C# Interface represents a contract, by advertising an Interface you are saying that you will implement the methods defined. With this in mind I'm confused why you would ever need to define an optional method. This is not slur or an attempt to lessen Objective-C, I love Objective-C. I simply want to understand the

Why ternary operator in swift is so picky?

匆匆过客 提交于 2019-12-04 02:56:21
问题 The question is very simple, but I just could not find the answer! Why doesn't return x == 0? "" : "Hello" compile but return x == 0 ? "" : "Hello" does? This is really weird because all the other operators don't need an extra white space. e.g. let x = 1+1 let y = 1 + 1 are the same. I think it has something to do with optionals. But when you use a ? operator on a variable, it must be used like this: let s: String? = nil let x = s?.startIndex I mean it must follow another operator, right? 回答1

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

ⅰ亾dé卋堺 提交于 2019-12-04 01:13:03
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 问题的解决