当前的SYB是否允许使用新类型扩展泛型函数? [英] Does the current SYB permit extension of generic functions with new types?

查看:51
本文介绍了当前的SYB是否允许使用新类型扩展泛型函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一 两个废弃样板文件描述了一种方法编写适用于通用类型的通用函数,但对于特定类型则具有特殊情况.例如, aeson 包中的 fromJSON 定义了用于从JSON转换的通用函数,但为list或 Int 等类型提供了特殊情况:

The first two Scrap Your Boilerplate papers describe a way of writing generic functions that work for general types, but have special cases for specific types. For instance, fromJSON from the aeson package, defines a generic function for converting from JSON, but provides special cases for types like list or Int:

parseJSON :: (Data a) => Value -> Parser a
parseJSON j = parseJSON_generic j
             `ext1R` list
             `ext1R` vector
             `ext2R'` mapAny
             `ext2R'` hashMapAny
             -- Use the standard encoding for all base types.
             `extR` (value :: F Integer)
             `extR` (value :: F Int)
              ...

但是,作为第三本SYB文件指出,"[所有类型特定的案例[需要]当泛型函数定义的递归结被束缚时立即提供".然后,本文提出了一种通过类型-类机制来解除此限制的方法.

However, as the third SYB paper notes, "all the type-specific cases [need] to be supplied at once, when the recursive knot of generic function definition is tied". The paper then proposes a way to lift this restriction through the type-class mechanism.

前两篇SYB论文(进行了一些修改)是 syb 的一部分.包,但第三个不是.通过SYB on Hackage的实施,是否还有其他方法可以解除所有必须同时指定所有特定类型案例的限制?

The first two SYB papers are (with some modifications) part of the syb package, but the third is not. Is there some other way of lifting the restriction that all type-specific cases need to be specified at once with the implementation of SYB on Hackage?

推荐答案

据我所知,没有办法解决该系统中的限制.事实并非如此,因此有必要在第三篇论文中介绍一种新方法.有可用的syb-with-class代码,实现了有关黑客的第三篇文章: http://hackage.haskell.org/package/syb-with-class

There's, as far as I know, no way around the restriction within that system. The fact that there isn't is what necessitates introducing a new approach in the third paper. There's working syb-with-class code, implementing the third paper on hackage: http://hackage.haskell.org/package/syb-with-class

但是,我最近的建议是尝试尝试新的泛型机制,该机制更快,更原则,更灵活(尽管有时会很痛苦:-).

However, my recommendation these days would be to try to experiment with the new Generics mechanism, which is faster, more principled, and more flexible (albeit occasionally a real pain :-)).

这篇关于当前的SYB是否允许使用新类型扩展泛型函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆