在 Idris 2 中选择命名实现

问题描述 投票:0回答:1

根据这个旧答案,Idris 1 具有

using
语法来选择代码块的默认实现。对应的 Idris 2 语法是什么?

我尝试使用答案中的语法:

using implementation Any
  tick1 : Time -> Writer Bool Time
  tick1 t = pure t

但是这个甚至无法解析:

 `-- src/HL2/Clock.idr line 14 col 0:
     Expected end of input.
     
     HL2.Clock:14:1--14:6
      14 | using implementation Any
           ^^^^^
syntax typeclass idris
1个回答
0
投票

@{name}
语法允许将自定义实现传递给函数。

[reverseOrd] Ord Nat where
   compare Z (S n)     = GT
   compare (S n) Z     = LT
   compare Z Z         = EQ
   compare (S x) (S y) = compare @{reverseOrd} x y


main : IO ()
main = printLn $ sort @{reverseOrd} [1,2,3,4,5]

此打印

[5, 4, 3, 2, 1]

© www.soinside.com 2019 - 2024. All rights reserved.