我为Int类型的type typeclass定义了一个命名实现。
[mijnOrd] Ord Int where
compare n1 n2 = ...
如何导入这个命名的实现并将其用作“默认”
--
sort [1,5,2] -- output without importing as default: [1,2,5]
sort [1,5,2] -- output with importing as default: [5,2,1]
这在伊德里斯有可能吗?
发布于 2016-07-14 23:49:01
这是可能的,因为Idris 0.12使用using
-blocks:
在一个模块中导出指定的接口,比如MyOrd.idr
module MyOrd
-- Reverse order for `Nat`
export
[myOrd] Ord Nat where
compare Z Z = EQ
compare Z (S k) = GT
compare (S k) Z = LT
compare (S k) (S j) = compare @{myOrd} k j
然后,只需将其导入另一个模块中,并在相应的using
-block中包装所有应该将其作为默认值使用的内容,如下所示:
-- Main.idr
module Main
import MyOrd
using implementation myOrd
test : List Nat -> List Nat
test = sort
main : IO ()
main = putStrLn $ show $ test [3, 1, 2]
这应该会打印[3, 2, 1]
。
https://stackoverflow.com/questions/37301403
复制相似问题