optional

How to use high order functions to filter and match on options fields in a record

醉酒当歌 提交于 2019-12-04 19:33:46
So a bit of context is required just to understand my issue. In an RPG, I consider the equipment as a record with optional fields, which means, while they're None that nothing was attributed yet. Equipment is constructed by the game items of the game that represents either weaponry or character protection (helmets and etc) which will be seen in the snippet below. The functionalities are removed and the domain model reduced to make it easier to read. type ConsummableItem = | HealthPotion | ManaPotion type Weaponry = | Sword | Spear | BattleAxe type CharaterProtection = | Helmet | Gloves | Boots

Parse JSON with optional field

纵然是瞬间 提交于 2019-12-04 19:29:48
I'm trying to parse in android studio a JSON, that containts this : "stops": [ { "num": 1, "time": "2016-04-27T06:15:00.000Z", "title":"Flight to London", "desc":"Barcelona BCN-London-Gatwick LGW", "type":"0", "subtype":0 }, { "num": 2, "time": "2016-04-27T10:35:00.000Z", "title":"Gatwick express", "desc":"From Airport to London", "type":"0", "subtype":1 }, { "num": 3, "time": "2016-04-27T12:15:00.000Z", "title":"Pub the black horse", "desc":"From Airport to London", "type":1, "subtype":1, "location": "51.476334, -0.062700", "images": [ "https://fitzrovianews.files.wordpress.com/2011/01/black

Get field of optional object or return null

浪尽此生 提交于 2019-12-04 19:16:20
问题 I have optional object: Optional<Detail> newestDetail; I would like to return newestDetail.getId() or if newestDetail is null return null . Do we have more sophisticated approach of doing this, than following? return newestDetail.isPresent()?newestDetail.get().getId():null; 回答1: Map the value to an Optional with the id field and turn that one into a null value if it is empty: return newestDetail.map(Detail::getId).orElse(null); 来源: https://stackoverflow.com/questions/47714105/get-field-of

java8-Optional类

荒凉一梦 提交于 2019-12-04 19:04:41
背景 NPE问题,100%的Java程序员都碰到,并且曾经是心中的痛。 1965年英国TonyHoare引入了Null引用,后续的设计语言包括Java都保持了这种设计。 一个例子 业务模型 Person 有车一族, 有Car字段, Car 车,每个车都有购买保险, 有Insurance字段; Insurance 保险,每个保险都有名字 有name字段; 需求:获取某个Person对象的购买保险的名称; 常规编程 public String getCarInsuranceName(Person person) { return person.getCar().getInsurance().getName(); } 检查式编程 public String getCarInsuranceName_check(Person person) { if (Objects.nonNull(person)) { final Car car = person.getCar(); if (Objects.nonNull(car)) { final Insurance insurance = car.getInsurance(); if (Objects.nonNull(insurance)) { return insurance.getName(); } } } return "unkown"; }

java8-Optional的引入

馋奶兔 提交于 2019-12-04 19:02:51
背景 NPE问题,100%的Java程序员都碰到,并且曾经是心中的痛。 1965年英国TonyHoare引入了Null引用,后续的设计语言包括Java都保持了这种设计。 一个例子 业务模型 Person 有车一族, 有Car字段, Car 车,每个车都有购买保险, 有Insurance字段; Insurance 保险,每个保险都有名字 有name字段; 需求:获取某个Person对象的购买保险的名称; 常规编程 public String getCarInsuranceName(Person person) { return person.getCar().getInsurance().getName(); } 检查式编程 public String getCarInsuranceName_check(Person person) { if (Objects.nonNull(person)) { final Car car = person.getCar(); if (Objects.nonNull(car)) { final Insurance insurance = car.getInsurance(); if (Objects.nonNull(insurance)) { return insurance.getName(); } } } return "unkown"; }

java8-Optional的引入

混江龙づ霸主 提交于 2019-12-04 18:58:26
背景 NPE问题,100%的Java程序员都碰到,并且曾经是心中的痛。 1965年英国TonyHoare引入了Null引用,后续的设计语言包括Java都保持了这种设计。 一个例子 业务模型 Person 有车一族, 有Car字段, Car 车,每个车都有购买保险, 有Insurance字段; Insurance 保险,每个保险都有名字 有name字段; 需求:获取某个Person对象的购买保险的名称; 常规编程 public String getCarInsuranceName(Person person) { return person.getCar().getInsurance().getName(); } 检查式编程 public String getCarInsuranceName_check(Person person) { if (Objects.nonNull(person)) { final Car car = person.getCar(); if (Objects.nonNull(car)) { final Insurance insurance = car.getInsurance(); if (Objects.nonNull(insurance)) { return insurance.getName(); } } } return "unkown"; }

Unwrap an Optional only if it is present

白昼怎懂夜的黑 提交于 2019-12-04 18:07:18
问题 So currently I have String uri = website.getUri(); Optional<PageDetail> pageDetail = webClient.getDetailOfUri(uri); String displayName; String description; if (pageDetail.isPresent()) { displayName = pageDetail.get().getName(); description = pageDetail.get().getDescription(); } else { displayName = uri; description = ""; } I am calling the getDetailOfUri(uri) method, which returns an Optional<PageDetail> , and I would like to set the strings displayName and description to the values of the

iOS/Swift: Can't assign optional String to UILabel text property

半城伤御伤魂 提交于 2019-12-04 17:58:15
UILabel has a text property, which is an optional String, but it seems to be behaving like an implicitly unwrapped optional. Why can't I assign it another optional String? Thanks. @IBOutlet weak var tweetContent: UILabel! ... var unopt: String = "foo" var opt: String? = "bar" var opt2: String? opt2 = opt //Works fine cell.tweetContent.text? = unopt //Works fine cell.tweetContent.text? = opt //Compile error: Value of optional type 'String?' not unwrapped You don't need to unwrap text . Leaving text as an String? (aka Optional<String> ) cell.tweetContent.text = unopt // Works: String implicitly

Optional chaining with Swift strings

做~自己de王妃 提交于 2019-12-04 17:47:33
问题 With optional chaining, if I have a Swift variable var s: String? s might contain nil, or a String wrapped in an Optional. So, I tried this to get its length: let count = s?.characters?.count ?? 0 However, the compiler wants this: let count = s?.characters.count ?? 0 My understanding of optional chaining is that, once you start using ?. in a dotted expression, the rest of the properties are made optional and are typically accessed by ?. , not . . So, I dug a little further and tried this in

accessing nested dictionary from api in swift

余生长醉 提交于 2019-12-04 17:41:58
Holy cow...there MUST be a better way to access formatted in floorplan_summary: { bedrooms: { low: 1, high: 2, formatted: "1 - 2 Beds" } } than doing this: if data["floorplan_summary"]?["bedrooms"] != nil { let bedrooms = data["floorplan_summary"]?["bedrooms"] as NSDictionary if bedrooms["formatted"] != nil{ self.beds = bedrooms["formatted"] as String } } I want to just do this: self.beds = data["floorplan_summary"]?["bedrooms"]?["formatted"] as String ..but at each level the object seems to be cast as AnyObject . Why can the compiler assume this data["floorplan_summary"]?["bedrooms"] but not