optional

Casting to generic optional in Swift

偶尔善良 提交于 2019-12-10 04:33:54
问题 I'm fiddling around with generics in Swift and hit something I can't figure out: If I cast a value into the type of a generic parameter, the cast is not performed. If I try the same with static types, it works. class SomeClass<T> { init?() { if let _ = 4 as? T { println("should work") } else { return nil } } } if let _ = SomeClass<Int?>() { println("not called") } if let _ = 4 as? Int? { println("works") } Can anybody explain this behavior? Shouldn't be both cases equivalent? Update The above

Markdown 中如何添加图片

荒凉一梦 提交于 2019-12-10 04:09:11
语法 ![alt text](uri "optional title") alt text:可选,图片标签,用来描述的关键词,图片加载错误时候的替代文本,也可用于SEO. uri: 可选,图片链接,可以是本地/网络/base64编码地址. optional title:可选,鼠标移动到图片上显示的标题. 本地图片 ![本地图片](/dic/logo.png) 本地图片可以使用绝对路径/相对路径,不便于分享. 网络图片 ![网络图片](https://some.png) 网络图片存在于你的/别人的服务器上,可通过网络地址直接引用,但依赖网络. base64编码图片 ![base64图片](data:image/png;base64,dajhahdfkf...省略不知道多少字符) 上面的base64图片用法,通常会文章中间出现一大长串base64字符,非常影响编写排版和复用,可以如下独立复用. ![base图片][image-base64Str] # .md 文章末尾统一写复用的内容 [image-base64Str]:data:image/png;base64,dsjkadd... 不过base64也有个问题,比如我要在多篇文章中使用同一个图片呢? So,大家看着用吧... 来源: oschina 链接: https://my.oschina.net/u/2912152/blog

Mockito error with method that returns Optional<T>

久未见 提交于 2019-12-10 00:40:19
问题 I have an interface with the following method public interface IRemoteStore { <T> Optional<T> get(String cacheName, String key, String ... rest); } The instance of the class implementing the interface is called remoteStore. When I mock this with mockito and use the method when: Mockito.when(remoteStore.get("a", "b")).thenReturn("lol"); I get the error: Cannot resolved the method 'thenReturn(java.lang.String)' I thought it has to do with the fact that get returns an instance of the Optional

Use of Optional in a map

给你一囗甜甜゛ 提交于 2019-12-09 17:53:59
问题 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

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

半城伤御伤魂 提交于 2019-12-09 17:24:04
问题 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

Can Optional ifPresent() be used in a larger expression to mitigate a call to get()?

↘锁芯ラ 提交于 2019-12-09 16:12:03
问题 To avoid calling get() which can throw an exception: if (a.isPresent()) list.add(a.get()); I can replace this expression with: a.ifPresent(list::add); But what if I need to perform a larger expression like: if (a.isPresent() && b && c) list.add(a.get()); Is it possible to still use a lambda form for this that mitigates a call to get() ? My use-case is to avoid get() entirely where possible to prevent a possible unchecked exception being missed. 回答1: My assumption is that you'd have to treat

How to map an OptionalLong to an Optional<Long>?

半城伤御伤魂 提交于 2019-12-09 15:15:44
问题 I have an instance of OptionalLong . But one of my libraries requires an Optional<Long> as a parameter. How can I convert my OptionalLong into an Optional<Long> ? I was dreaming about something like this: OptionalLong secondScreenHeight = OptionalLong.of(32l); // or: OptionalLong.empty() api.setHeight(secondScreenHeight.maptoRegularOptional()); // .maptoUsualOptional does not exist 回答1: I don't know simpler solutions but this will do what you need. OptionalLong secondScreenHeight =

Groovy Closure with optional arguments

孤街浪徒 提交于 2019-12-09 07:48:00
问题 I want to define a closure which takes one argument (which i refer to with it ) sometimes i want to pass another additional argument to the closure. how can i do this? 回答1: You could set the second argument to a default value (such as null): def cl = { a, b=null -> if( b != null ) { print "Passed $b then " } println "Called with $a" } cl( 'Tim' ) // prints 'Called with Tim' cl( 'Tim', 'Yates' ) // prints 'Passed Yates then Called with Tim Another option would be to make b a vararg List like

Why should optional<T&> rebind on assignment?

霸气de小男生 提交于 2019-12-09 02:18:48
问题 There is an ongoing debate about what optional and variant should do with reference types, particularly with regards to assignment. I would like to better understand the debate around this issue. optional<T&> opt; opt = i; opt = j; // should this rebind or do i=j? Currently, the decision is to make optional<T&> ill-formed and make variant::operator= ill-formed if any of the types is a reference type - to sidestep the argument and still give us most of the functionality. What is the argument

check if any property in an object is nil - Swift 3

谁都会走 提交于 2019-12-09 01:02:01
问题 Im using Swift 3 Wondering whether any method is available to check if all the properties in an object has value / nil Eg: class Vehicle { var name : String? var model: String? var VIN: String? } let objCar = Vehicle() objCar.name = "Volvo" if objCar.{anyProperty} ! = nil { //Go to other screen } Im in search of the {anyProperty} method where it returns true only if I have values for all properties of objCar. In our case, objCar has no model and VIN and so {anyProperty} is false and will come