如何在Clojure';s核心类型?
我正在做一个井字游戏,并且已经为我的策略制定了一个协议。游戏运行良好,所以我想借此机会磨练我的核心技能。我已经对协议进行了注释(如下所示),但是当我在repl中运行如何在Clojure';s核心类型?,clojure,type-systems,lighttable,clojure-core.typed,Clojure,Type Systems,Lighttable,Clojure Core.typed,我正在做一个井字游戏,并且已经为我的策略制定了一个协议。游戏运行良好,所以我想借此机会磨练我的核心技能。我已经对协议进行了注释(如下所示),但是当我在repl中运行(cf method name)或(cf protocol name)时,会出现以下错误: 例如: 我怀疑在使用cf查询当前名称空间之前,您需要检查ns名称空间。类型化代码的求值不会触发类型检查或注释集合 这里不需要ann协议,clojure.core.typed/defprotocol支持内联注释 尝试以下方法: (defproto
(cf method name)
或(cf protocol name)
时,会出现以下错误:
例如:
我怀疑在使用
cf
查询当前名称空间之前,您需要检查ns
名称空间。类型化代码的求值不会触发类型检查或注释集合
这里不需要ann协议
,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")
...)
谢谢安布罗斯!我来试一试。这些方法的输入类型是推断出来的吗?“self”类型是推断出来的,并且不能直接在defprotocol中被重写(还!)。对于非多态协议,“self”是策略,否则它是(策略AB c),假设协议由a、b和c参数化。但是,第一个之后的所有参数都不会被推断出来。这是有意义的。但是,如果我返回nil或一个策略方法,我会给它加上注释:-(U nil[nil])还是什么?
(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? [_]))
(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")
...)