optional

Preferred approach to check optionals in Swift

和自甴很熟 提交于 2019-12-24 07:05:13
问题 In working with optionals in Swift, there seems to be two approaches to check if an optional type is nil or not. var item: String? = "apple" // Approach A if item != nil { "item is \(item!)" } else { "no item" } // Approach B if let x = item { "item is " + x } else { "no item" } Does it matter which approach I use to check the optional? 回答1: They are equivalent, but it's better to use: let x = item (optional binding) when you actually need and use the unwrapped value item != nil when you just

Preferred approach to check optionals in Swift

烈酒焚心 提交于 2019-12-24 07:05:09
问题 In working with optionals in Swift, there seems to be two approaches to check if an optional type is nil or not. var item: String? = "apple" // Approach A if item != nil { "item is \(item!)" } else { "no item" } // Approach B if let x = item { "item is " + x } else { "no item" } Does it matter which approach I use to check the optional? 回答1: They are equivalent, but it's better to use: let x = item (optional binding) when you actually need and use the unwrapped value item != nil when you just

ModelMapper handling java 8 Optional<MyObjectDto> fields to Optional<MyObject>

对着背影说爱祢 提交于 2019-12-24 05:10:16
问题 I've been using modelmapper and java 8 Optionals all around the application which was working fine because they were primitive types; until I changed one of my model objects' field to Optional type. Then all hell broke loose. Turns out many libraries cannot handle generics very well. Here is the structure public class MyObjectDto { private Optional<MySubObjectDto> mySubObject; } public MyObject { privae Optional<MySubjObject> mySubObject; } When I attempt to map MyObjectDto to MyObject ,

Java 8 ifPresent vs ternary operator

对着背影说爱祢 提交于 2019-12-24 03:36:07
问题 What do you think is better (with arguments, of course): Optional.ofNullable( userName ) .ifPresent( nonNullUserName -> header.setUser( createUser( nonNullUserName ) ) ); or header.setUser( userName == null ? createUser( userName ) : null ); The method createUser creates xml element and the intent of the whole peace of code is to set it in a SOAP request depending on presence of userName . The benefits of the first approach I see is the absence of useless operations, the code does one thing

regex c# optional group - should act greedy?

此生再无相见时 提交于 2019-12-24 02:48:36
问题 having regex ~like this: blablabla.+?(?:<a href="(http://.+?)" target="_blank">)? I want to capture an url if I find one... finds stuff but I don't get the link (capture is always empty). Now if I remove the question mark at the end like this blablabla.+?(?:<a href="(http://.+?)" target="_blank">) This will only match stuff that has the link at the end... it's 2.40 am... and I've got no ideas... --Edit-- sample input: blablabla asd 1234t535 <a href="http://google.com" target="_blank">

regex c# optional group - should act greedy?

北城余情 提交于 2019-12-24 02:48:11
问题 having regex ~like this: blablabla.+?(?:<a href="(http://.+?)" target="_blank">)? I want to capture an url if I find one... finds stuff but I don't get the link (capture is always empty). Now if I remove the question mark at the end like this blablabla.+?(?:<a href="(http://.+?)" target="_blank">) This will only match stuff that has the link at the end... it's 2.40 am... and I've got no ideas... --Edit-- sample input: blablabla asd 1234t535 <a href="http://google.com" target="_blank">

Semantics of optional in the context of Transient Attributes in CoreData

淺唱寂寞╮ 提交于 2019-12-24 01:53:26
问题 What difference does it make if a transient attribute in core data is set to optional or non optional. Normally when you set an entity to being optional, it allows the entity owning that attribute to be stored with that attribute set to null. But in the case of transient attributes they aren't actually stored. So does setting a transient property to optional do anything? Do you need to set a transient attribute to optional if it is calculated using an optional non-transient property? Any

Semantics of optional in the context of Transient Attributes in CoreData

耗尽温柔 提交于 2019-12-24 01:52:05
问题 What difference does it make if a transient attribute in core data is set to optional or non optional. Normally when you set an entity to being optional, it allows the entity owning that attribute to be stored with that attribute set to null. But in the case of transient attributes they aren't actually stored. So does setting a transient property to optional do anything? Do you need to set a transient attribute to optional if it is calculated using an optional non-transient property? Any

Maven 中<optional>true</optional>和<scope>provided</scope>之间的区别

女生的网名这么多〃 提交于 2019-12-23 18:16:34
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 依赖管理是maven提供的主要功能之一。无论我们需要什么依赖,我们只需将它们添加到POM.xml中。由于maven,所有必要的类和资源都会自动添加到项目的classpath中。 在添加依赖项时,我们可以使用optional标志,或将scope设置为“provided”。在这两种情况下,依赖关系都将在声明它们的模块的classpath中,但是使用将它们定义为依赖关系的模块不会在其他项目中传递它们,即不会形成依赖传递。 从语义来上理解 optional 可选的,可以理解为此功能/此依赖可选,如果不需要某项功能,可以不引用这个包。 scope provided 提供的,可以理解为此包不由我直接提供,需要调用者/容器提供。 举个例子说明二者的使用场景和区别 optional 现开发了一个类似Hibernate的框架,叫Summer吧,致敬下Spring,提供了多种数据库方言的支持:mysql/oracle/db2/postgresql... 每种数据库支持也独立了一个module,Summer的依赖中配置了每种数据库的支持包:summer-mysql-support/summer-oracle-support... 但是实际引用此框架/依赖时,并不需要所有数据库方言的支持。此时可以把数据库的支持包都配置为可选的

Behaviour of greater than (and equivalent) for Option

孤街醉人 提交于 2019-12-23 17:24:07
问题 I'm working on something for an image processing task (it's not greatly relevant here), and I stumbled across behaviour of F#'s Option type that surprised me, regarding performing greater than (>) comparisons. I couldn't find anything that directly explained what I should expect (more on that below) on Stack Overflow, the F# docs or the wider web. The specific part I am looking at looks something like: let sort3Elems (arr: byte option []) = if arr.[0] > arr.[1] then swap &arr.[0] &arr.[1] if