www.91084.com

GVKun编程网logo

swift-模式匹配 && override ~= operator(swift配对)

2

本篇文章给大家谈谈swift-模式匹配&&override~=operator,以及swift配对的知识点,同时本文还将给你拓展@Overridemustoverrideasuperclassmeth

本篇文章给大家谈谈swift-模式匹配 && override ~= operator,以及swift配对的知识点,同时本文还将给你拓展@Override must override a superclass method、@Override 报 must override a superclass method 错、AppCode 2017.1:支持 Swift 3,改进 Override/Implement、c – 静态与成员运算符重载:std :: operator <<和std :: ostream :: operator <<等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

swift-模式匹配 && override ~= operator(swift配对)

swift-模式匹配 && override ~= operator(swift配对)

Swift的switch使用 ~= 操作符进行模式匹配,case A, A是~=操作符的左参数,switch B, B是~=操作符的右参数。swift隐式调用 ~=操作符。

所以自定义模式匹配的方法如下:

e.g.1

enum Day: Int {
    case Monday, TuesDay, Wednesday, Thursday, Friday, Saturday, Sunday
}
func ~= (lhs:Int, rhs:Day) ->Bool {
    return lhs == rhs.rawValue + 1
}

class Tutorial {
    let day:Day?
    ...
}

extension Tutorial {
    var order: String {
        guard let day = day else {
            return "not scheduled"
        }
        
        switch day {
        case 1:
            return "first"
        case 2:
            return "second"
        case 3:
            return "third"
        case 4:
            return "fourth"
        case 5:
            return "fifth"
        case 6:
            return "sixth"
        case 7:
            return "seventh"
        default:
            fatalError("invalid")
        }
        
    }
} 

 

e.g.2

http://swifter.tips/pattern-match/

@Override must override a superclass method

@Override must override a superclass method

在用Eclipse开发Java项目时,@Override 注解时会出现以下错误提示:
The method *** of type *** must override a superclass method

主要是因为该项目的Compiler comliance level的值设置不正确造成的,5.0是不支持@Override等注解,只要把它改为6.0就可以了,当然或者你也可以直接把注解@Override去掉。

修改Compiler comliance level的方法(两个):

方法一:将window->preferences->Java Compiler,找到选项:Compiler compliance level,修改相应的值即可,注意此时是一个全局的修改,当然也可以为某个项目单独设置。如下图显示:

方法二:右击项目->Properties->Java Compiler,找到选项:Compiler compliance level,修改相应的值即可。

@Override 报 must override a superclass method 错

@Override 报 must override a superclass method 错

原因是因为偶的Eclipse 默认的 Compiler是jdk5,(5不支持@Override等形式的批注), 只要把它改为1.7以上就可以了。
方法:将window->preferences->java-compiler中的Compiler compliance level修改为1.7。

1.  

2. 修改

如还不能解决: 
找到现有的工程目录:

1. 找到工程

2. 修改为:1.7

AppCode 2017.1:支持 Swift 3,改进 Override/Implement

AppCode 2017.1:支持 Swift 3,改进 Override/Implement

AppCode 2017.1 正式发布了,新版更好的支持 Swift 3,也改进了 Override/Implement。

总体更新如下:

  • Swift:

    • 更好的语言支持,包括解决方案,完成和导航方面的许多改进。

    • Create from usage for types and initializers.

    • 改进 Override/Implement

    • 改进 Completion

    • 在 Structure 视图支持 //MARK, //FIXME, and //TODO 注释标签

  • C++: 新的 C++14 和 C++17 特性

  • 在 “构建消息” 工具窗口中改进过滤

  • 类似 Xcode 的断点

  • 在编辑器和 IDE 视图中支持 Emoji

  • And more.

详细更新内容请参阅 发布主页。

下载地址

c – 静态与成员运算符重载:std :: operator <<和std :: ostream :: operator <<

c – 静态与成员运算符重载:std :: operator <<和std :: ostream :: operator <<

C的ostream类为运算符<<提供了许多默认重载,但它们并非都以相同的方式定义. char类型,字符串类型和rvalue流的 overloads定义为免费的命名空间范围函数,例如:
namespace std {
ostream &operator<<(ostream &os,char c);
}

而算术类型,streambuf和流操作符的overloads被定义为std :: ostream的成员函数,例如:

namespace std {
ostream &ostream::operator<<(int val);
}

我的问题

有这种区别的原因吗?我理解对这些运算符重载的调用操作稍有不同(即空闲命名空间范围定义的ADL),因此我认为出于优化目的,可能会优先考虑特定类型的运算符重载.但是这里std :: ostream对不同类型使用两种类型的定义.这允许的语义或实现优化是否有任何优势?

解决方法

I’d imagine there might be a preference to a particular type of operator overload for optimization purposes

好吧,不.在一天结束时,两者都作为函数调用执行.重载决策本身甚至没有明显的含义.由于标准规定在[over.match],第2和6段:

If either operand has a type that is a class or an enumeration,a
user-defined operator function might be declared that implements this
operator or a user-defined conversion can be necessary to convert the
operand to a type that is appropriate for a built-in operator. In this
case,overload resolution is used to determine which operator function
or built-in operator is to be invoked to implement the operator.

The set of candidate functions for overload resolution is the union of
the member candidates,the non-member candidates,and the built-in
candidates. The argument list contains all of the operands of the
operator. The best function from the set of candidate functions is
selected according to [over.match.viable] and [over.match.best].

所有这些运算符重载都一起解决.唯一的语义差异是从ostream派生的类可能会选择隐藏某些成员重载.这是根据派生类中的重载方式完成的.只有明确声明的重载才适用.与那些成员不同,自由函数重载将始终参与重载解析,即使对于从ostream派生的类也是如此.

由于派生类需要转换为ostream&为了选择自由函数重载,需要对其自己的隐式转换序列进行排序.如果所有重载都是自由函数,则可能会导致歧义.

因此,考虑很可能是将可能导致歧义(指针和算术类型)的类型与我们可能总是希望可用的有用类型(指向C字符串和单个字符的指针)分开.并允许隐藏“不太有用”的那些,以避免这些含糊不清.

正如W.F.所指出的那样. ostream实际上是basic_ostream< char>.自由函数恰好适用于仅需要流式传输的数据.流本地“字母”中的字符或字符串.所以对于basic_ostream< wchar_t>那些自由函数将接受wchar_t和wchar_t *.简单的流媒体很可能不需要访问流私有部分.

其他重载适用于在流式传输之前需要序列化的数据.由于所述序列化与流内部状态紧密耦合,因此使这些重载成为成员更有意义.

今天关于swift-模式匹配 && override ~= operatorswift配对的介绍到此结束,谢谢您的阅读,有关@Override must override a superclass method、@Override 报 must override a superclass method 错、AppCode 2017.1:支持 Swift 3,改进 Override/Implement、c – 静态与成员运算符重载:std :: operator <<和std :: ostream :: operator <<等更多相关知识的信息可以在本站进行查询。

本文标签: