
[英]How do you annotate polymorphic core functions in Clojure's core.typed?
[英]How do I annotate protocols and their methods in Clojure's core.typed?
我正在打井字游戏,并为我的策略制定了协议。 游戏运行良好,所以我想借此机会磨练我的core.typed技能。 我已经注释了协议(如下所示),但是当我在(cf method-name)
运行(cf method-name)
或(cf protocol-name)
,出现此错误:
例如:
=> `(cf win)`
clojure.lang.ExceptionInfo: Type Checker: Found 1 error :: {:type-error :top-level-error, :errors (#<ExceptionInfo clojure.lang.ExceptionInfo: Unannotated var tic-tac-toe.protocol/win
Hint: Add the annotation for tic-tac-toe.protocol/win via check-ns or cf {:env {:file "/Users/jessediaz/Documents/workspace/tic-tac-toe/src/tic_tac_toe/protocol.clj", :column 5, :line 47}, :form win, :type-error :clojure.core.typed.errors/tc-error-parent}>)}
我已经检查以确保协议的版本实际上是core.typed / protocol。 当我使用protocol>时,控制台对我咆哮,说语法已过时。 我也浏览了github页面和clojure-doc.org上的文档。 这是我了解到有一个可选的:methods
关键字,可以用来将方法映射到类型的方法。 我觉得这可能会从我的代码中消除很多重复,但是我找不到任何使用它的示例。 该协议中的主要策略方法都有副作用,且均为nil
。 验证方法返回nil
或它们验证的原始Strategy
方法。 我不确定我那里是否有语法,但是无论是否有Fn
签名,代码在LightTable中的评估都很好,它们的core.typed Wiki表示并非总是必要的。
我觉得我在学习这个令人惊叹的库方面正处于风口浪尖,并且希望能提供任何有帮助我理解的有见地的建议。
protocol.clj文件:
(ns tic-tac-toe.protocol
(:require [clojure.core.typed :refer :all]))
(ann-protocol Strategy
win (Fn [Strategy -> nil])
block (Fn [Strategy -> nil])
fork (Fn [Strategy -> nil])
block-fork (Fn [Strategy -> nil])
take-center (Fn [Strategy -> nil])
take-opposite-corner (Fn [Strategy -> nil])
take-corner (Fn [Strategy -> nil])
take-side (Fn [Strategy -> nil])
can-win? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-block? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-fork? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-block-fork? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-take-center? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-take-corner? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))])
can-take-side? (Fn [Strategy -> (U nil (Fn [Strategy -> nil]))]))
(defprotocol Strategy
"Strategy methods update the Tic-tac-toe board when they are called"
;; strategies
(win [_] "wins the game by filling an open space to get 3 in a row")
(block [_] "blocks an opponents win by filling an open space")
(fork [_] "creates a two way win scenario guaranteeing victory")
(block-fork [_] "prevents an opponent from forking")
(take-center [_] "takes center")
(take-opposite-corner [_] "takes a corner opposite to one the computer already has")
(take-corner [_] "takes an avaiable corner")
(take-side [_] "takes an available side")
;; tests
(can-win? [_])
(can-block? [_])
(can-fork? [_])
(can-block-fork? [_])
(can-take-center? [_])
(can-take-opposite-corner? [_])
(can-take-corner? [_])
(can-take-side? [_]))
我怀疑您需要check-ns
当前名称空间,然后再使用cf
进行查询。 评估类型代码不会触发类型检查或注释集合。
此处不需要ann-protocol
, clojure.core.typed/defprotocol
支持内联注释。
尝试类似:
(defprotocol Strategy
"Strategy methods update the Tic-tac-toe board when they are called"
;; strategies
(win [_] :- nil "wins the game by filling an open space to get 3 in a row")
(block [_] :- nil "blocks an opponents win by filling an open space")
...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.