1

我正在制作井字游戏,并为我的策略制定了协议。游戏运行良好,所以我想借此机会磨练我的 core.typed 技能。我已经对协议进行了注释(如下所示),但是当我运行(cf method-name)(cf protocol-name)在 repl 中时,我得到了这个错误:

例如:

=> `(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我不确定我的语法是否正确,但代码在 LightTable 中的评估效果很好,有或没有Fn签名,他们 core.typed wiki 暗示它并不总是必要的。

我觉得我正处于学习这个令人惊叹的图书馆的风口浪尖,并且希望任何可以帮助我理解的有见地的建议。

协议.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? [_]))
4

1 回答 1

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")
  ...)
于 2014-12-08T00:50:46.027 回答