在这篇文章中,我们将带领您了解函数式编程-TypeClass介绍的全貌,包括函数typedef的相关情况。同时,我们还将为您介绍有关c–模板,decltype和非classtypes、com.sun.
在这篇文章中,我们将带领您了解函数式编程 - Type Class 介绍的全貌,包括函数typedef的相关情况。同时,我们还将为您介绍有关c – 模板,decltype和非classtypes、com.sun.tools.classfile.Type.ClassSigType的实例源码、com.sun.tools.classfile.Type.ClassType的实例源码、com.sun.tools.classfile.Type.TypeParamType的实例源码的知识,以帮助您更好地理解这个主题。
本文目录一览:- 函数式编程 - Type Class 介绍(函数typedef)
- c – 模板,decltype和非classtypes
- com.sun.tools.classfile.Type.ClassSigType的实例源码
- com.sun.tools.classfile.Type.ClassType的实例源码
- com.sun.tools.classfile.Type.TypeParamType的实例源码
函数式编程 - Type Class 介绍(函数typedef)
什么是 Type Class ?
Type Class (类型类) 的概念来自 Haskell,表示一系列函数的集合,在概念上, Type Class 和面向对象领域的泛型接口比较类似。
由于 Haskell 是一门纯函数式编程语言,没有类和接口的概念,所以使用 Type Class 表达类似接口的概念。例如 Haskell 的 Ord 类型类在概念上和 Java 的 Comparable 非常类似。
在 Haskell 中,Type Class 使用 class
关键字定义:
class BasicEq a where isEqual :: a -> a -> Bool isEqual x y = not (isNotEqual x y) isNotEqual :: a -> a -> Bool isNotEqual x y = not (isEqual x y)
Type Class Instance 则使用 instance
关键字定义:
instance BasicEq Bool where isEqual False False = True isEqual True True = True isEqual _ _ = False
只要定义了 Bool 类型的 Type Class Instance,我们就可以在任何地方使用 isEqual 方法比较 Bool 类型的相等性。用面向对象的语言描述就是:Bool
类型实现了 BasicEq
接口。
Type Class 的好处是,当系统增加一个新类型时,只需要实现该新类型的 Type Class Instance 即可,已有类型代码可以保持不变。
Java中的 Type Class
在面向对象领域使用接口解决类似 Type Class 的问题,例如 Java 的 Comparable 接口。但是接口的方式并不优雅,它要求新类型必须实现 Comparable 接口,如果这个新类型来自第三方库,我们无法强制要求它实现 Comparable 接口。所以在面向对象领域,我们仍然可以借鉴 Type Class 的方式以解决接口所面临的问题。所以 Java 提供了一个 Type Class 的等价物,即 Comparator<T>
接口,所有实现了Comparator<T>
接口的实例即为 Type Class Instance。针对一个新类型Person
,我们只需要实现一个Comparator<Person>
实例,即可以在任何地方对Person
集合进行排序。
Scala 中的 Type Class
同样的,Scala 的核心库设计中也采用了 Type Class 的概念。例如Ordering[T]
是一个 Type Class, IntOrdering
是一个 Type Class Instance。所以只要有IntOrdering
实例,我们就可以对Int类型进行排序:
def sorted[B >: A](implicit ord : scala.math.Ordering[B])
由于Ordering
中已经预定了常见的Type Class Instance 的隐式对象,所以我们可以在任何地方对常见类型进行排序。
c – 模板,decltype和非classtypes
template <typename T> auto print(T t) -> decltype(t.print()) { return t.print(); }
这个想法是参数必须是T类型,并且必须具有print函数.这个打印功能可以返回任何内容,解释了decltype的需要.例如,您可以这样做:
struct Foo { int print() { return 42; } }; struct Bar { std::string print() { return "The answer..."; } }; ... std::cout << print(Foo()) << std::endl; std::cout << print(Bar()) << std::endl; /* outputs: 42 The answer... */
我读到模板不能进行运行时实例化,并且您可以从类派生类,然后确定它们的类型以查看要使用的模板参数.但是,我如何为非类型类型执行此操作?这个想法是能够:
template <typename T> T print(T t) { return t; }
同样,但这给了我模糊的过载错误.资格认证不起作用,即打印< Foo>.另一个得到的问题是,如果我有一个类似的仿函数:
struct Foo { virtual int print(); operator int() const { return 42; } };
它现在如何决定?
所以我的问题是,是否可以使用模板解决所有这些歧义,或者我是否必须编写一堆冗余代码?
测试
我逐步添加测试,从下面复制/粘贴每个编辑的解决方案.结果如下:
使用以下课程:
struct Foo { int print() { return 42; } operator int() const { return 32; } }; struct Bar { std::string print() { return "The answer..."; } operator int() const { return (int)Foo(); } }; struct Baz { operator std::string() const { return std::string("The answer..."); } };
以下测试输出:
std::cout << print(Foo()) << std::endl; std::cout << print(Bar()) << std::endl; std::cout << print(42) << std::endl; std::cout << print((int)Foo()) << std::endl; std::cout << print("The answer...") << std::endl; std::cout << print(std::string("The answer...")) << std::endl; std::cout << print((int)Bar()) << std::endl; std::cout << print((std::string)Baz()) << std::endl;
两者都正确输出:
42 The answer... 42 32 The answer... The answer... 32 The answer...
解决方法
namespace detail { template<typename T,typename = void> struct print_helper { static T print(T t) { return t; } }; template<typename T> struct print_helper<T,decltype(std::declval<T>().print(),(void)0)> { static auto print(T t) -> decltype(t.print()) { return t.print(); } }; } template<typename T> auto print(T t) -> decltype(detail::print_helper<T>::print(t)) { return detail::print_helper<T>::print(t); }
这是一个live example.
com.sun.tools.classfile.Type.ClassSigType的实例源码
public StringBuilder visitClassSigType(ClassSigType type,StringBuilder sb) { appendIfNotEmpty(sb,"<",type.typeParamTypes,">"); if (isInterface) { appendIfNotEmpty(sb," extends ",type.superinterfaceTypes,""); } else { if (type.superclasstype != null && (options.verbose || !type.superclasstype.isObject())) { sb.append(" extends "); append(sb,type.superclasstype); } appendIfNotEmpty(sb," implements ",""); } return sb; }
@Override public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,Void p) { StringBuilder sb = new StringBuilder(); sb.append("CS{"); if (type.typeParamTypes != null) sb.append(print("<",">")); sb.append(print(type.superclasstype)); if (type.superinterfaceTypes != null) sb.append(print("i(",")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClassSigType(ClassSigType type,""); } return sb; }
public String visitClassSigType(ClassSigType type,")")); sb.append("}"); return sb.toString(); }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
public Void visitClassSigType(ClassSigType type,Void p) { findDependencies(type.superclasstype); findDependencies(type.superinterfaceTypes); return null; }
com.sun.tools.classfile.Type.ClassType的实例源码
public StringBuilder visitClasstype(Classtype type,StringBuilder sb) { if (type.outerType != null) { append(sb,type.outerType); sb.append("."); } sb.append(getJavaName(type.name)); appendIfNotEmpty(sb,"<",type.typeArgs,">"); return sb; }
@Override public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,Void p) { StringBuilder sb = new StringBuilder(); sb.append("C{"); if (type.outerType != null) { sb.append(print(type.outerType)); sb.append("."); } sb.append(type.name); if (type.typeArgs != null) sb.append(print("<",">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public StringBuilder visitClasstype(Classtype type,">"); return sb; }
public String visitClasstype(Classtype type,">")); sb.append("}"); return sb.toString(); }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.getBinaryName()); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
public Void visitClasstype(Classtype type,Void p) { findDependencies(type.outerType); addDependency(type.name); findDependencies(type.typeArgs); return null; }
com.sun.tools.classfile.Type.TypeParamType的实例源码
public StringBuilder visitTypeParamType(TypeParamType type,StringBuilder sb) { sb.append(type.name); String sep = " extends "; if (type.classBound != null && (options.verbose || !type.classBound.isObject())) { sb.append(sep); append(sb,type.classBound); sep = " & "; } if (type.interfaceBounds != null) { for (Type bound: type.interfaceBounds) { sb.append(sep); append(sb,bound); sep = " & "; } } return sb; }
@Override public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public StringBuilder visitTypeParamType(TypeParamType type,bound); sep = " & "; } } return sb; }
public String visitTypeParamType(TypeParamType type,Void p) { StringBuilder sb = new StringBuilder(); sb.append("TA{"); sb.append(type.name); if (type.classBound != null) { sb.append(":c"); sb.append(print(type.classBound)); } if (type.interfaceBounds != null) sb.append(print(":i",type.interfaceBounds,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
public String visitTypeParamType(TypeParamType type,"")); sb.append("}"); return sb.toString(); }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"<",typeParamTypes,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
String printTypeArgs(List<? extends TypeParamType> typeParamTypes) { StringBuilder builder = new StringBuilder(); appendIfNotEmpty(builder,"> "); return builder.toString(); }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
public Void visitTypeParamType(TypeParamType type,Void p) { findDependencies(type.classBound); findDependencies(type.interfaceBounds); return null; }
今天的关于函数式编程 - Type Class 介绍和函数typedef的分享已经结束,谢谢您的关注,如果想了解更多关于c – 模板,decltype和非classtypes、com.sun.tools.classfile.Type.ClassSigType的实例源码、com.sun.tools.classfile.Type.ClassType的实例源码、com.sun.tools.classfile.Type.TypeParamType的实例源码的相关知识,请在本站进行查询。
本文标签: