zoukankan      html  css  js  c++  java
  • Optional是以enum和泛型为基础的高阶类型

    结论:1、optionals使用时需要检查;2、可以通过!+赋值语句转化为非optionals。

    Optional-Generic Enumeration

    enum Optional<T> : LogicValue, Reflectable {
      case None
      case Some(T)
      init()
      init(_ some: T)
     
      /// Allow use in a Boolean context.
      func getLogicValue() -> Bool
     
      /// Haskell's fmap, which was mis-named
      func map<U>(f: (T) -> U) -> U?
      func getMirror() -> Mirror
    }
     

    A type that represents either a wrapped value or nil, the absence of a value.

    The types of shortForm and longForm in the following code sample are the same:

    let shortForm: Int? = Int("42")

    let longForm: Optional<Int> = Int("42")

    Optionals

    ?!

    You use optionals in situations where a value may be absent. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all.

    optionals indicate that a constant or variable is allowed to have “no value”. Optionals can be checked with an if statement to see if a value exists, and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.

    1. let possibleString: String? = "An optional string."
    1. let possibleString: String? = "An optional string."
    2. let forcedString: String = possibleString! // requires an exclamation mark
  • 相关阅读:
    快速排序
    C# String.Format
    理解C++ static
    程序地址空间
    map的实现
    【S4】使用empty()而不是判断size()是否为0
    RHEL6.4 NFS文件共享服务器搭建
    使用UDEV绑定ASM多路径磁盘
    MySQL的启动程序
    [ERROR] Fatal error: Please read "Security" section of the manual to find out how to run mysqld as root!
  • 原文地址:https://www.cnblogs.com/feng9exe/p/8709210.html
Copyright © 2011-2022 走看看