我正在制作一个tic tac toe游戏并为我的策略制定了一个协议。游戏运行良好,所以我想借此机会磨练我的核心技能。我已经注释了协议(如下所示),但是当我在repl中运行(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。当我使用协议时,控制台对我咆哮&gt;说语法已被弃用。我还查看了github页面和clojure-doc.org上的文档。这就是我了解到有一个可选的:methods
关键字,我可以用它将方法映射到类型。我觉得这可能会从我的代码中删除很多重复,但我还没有找到任何使用它的例子。协议中的主要策略方法都有副作用并返回nil
。验证方法返回nil
或他们验证的原始Strategy
方法。我不确定我的语法是否正确,但代码在LightTable中的评估很好,有或没有Fn
签名,而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? [_]))
答案 0 :(得分:2)
我怀疑在使用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")
...)