我已经看到很多地方用Clojure项目中的某些依赖项标记了:scope "provided"(示例).
这是什么意思?
这有两个功能:
fn foo<I>(iter: &mut I)
where
I: std::iter::Iterator<Item = u8>,
{
let x = iter.by_ref();
let y = x.take(2);
}
fn bar<I>(iter: &mut I)
where
I: std::io::Read,
{
let x = iter.by_ref();
let y = x.take(2);
}
Run Code Online (Sandbox Code Playgroud)
虽然第一个编译正常,第二个编译错误:
error[E0507]: cannot move out of borrowed content
--> src/lib.rs:14:13
|
14 | let y = x.take(2);
| ^ cannot move out of borrowed content
Run Code Online (Sandbox Code Playgroud)
签名by_ref和take几乎完全相同std::iter::Iterator和std::io::Read特征,所以我认为如果第一个编译,第二个也将编译.我哪里弄错了?
您能用实例解释retag参数如何影响multi-spec创建?我觉得multi-spec文档难以消化.
假设我在 Rust 代码中有以下定义:
#[wasm_bindgen]
pub struct RustType {
foo: usize
}
#[wasm_bindgen]
impl RustType {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { foo: 100 }
}
}
#[wasm_bindgen]
pub fn print_addr(obj: &RustType) {
console_log!("rust addr = {}", obj as *const _ as u32);
}
Run Code Online (Sandbox Code Playgroud)
JS 代码创建一个实例RustType并将其传递给print_addr函数:
var obj = new RustType();
print_addr(obj);
Run Code Online (Sandbox Code Playgroud)
修改生成的print_addr函数后index_bg.js如下:
export function print_addr(obj) {
_assertClass(obj, RustType);
console.log("js addr = ", obj.ptr); // <== added this line
if …Run Code Online (Sandbox Code Playgroud) 为什么我能这样做:
> (set! *unchecked-math* true)
true
> (set! *warn-on-reflection* false)
false
Run Code Online (Sandbox Code Playgroud)
但不能这样做:
> (def ^:dynamic *x*)
#'user/*x*
> (set! *x* 1) ;; no luck, exception!
Run Code Online (Sandbox Code Playgroud) 假设我有以下类声明:
(defclass foo-class ()
((bar :initarg :bar
:type list)))
Run Code Online (Sandbox Code Playgroud)
当我创建这个类的实例时,make-instance不会检查传递的参数是否满足槽的类型.所以,我可以这样创建"无效"对象:
> (make-instance 'foo-class :bar 'some-symb)
#<FOO-CLASS {102BEC5E83}>
Run Code Online (Sandbox Code Playgroud)
但是,我想看到的是类似于创建结构实例的行为,其中检查了类型:
(defstruct foo-struct
(bar nil :type list))
> (make-foo-struct :bar 'some-symb)
;; raises contition:
;;
;; The value
;; SOME-SYMB
;; is not of type
;; LIST
;; when setting slot BAR of structure FOO-STRUCT
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
我正在尝试重现 Chas 描述的动态变量的陷阱 - http://cemerick.com/2009/11/03/be-mindful-of-clojures-binding/。
考虑以下片段:
(def ^:dynamic *dynamic-x* 10)
(defn adder [arg]
(+ *dynamic-x* arg))
(adder 5) ;; returns 15
(binding [*dynamic-x* 20]
(adder 5)) ;; returns 25
(binding [*dynamic-x* 20]
@(future (adder 5))) ;; returns 25 (!)
Run Code Online (Sandbox Code Playgroud)
实际上,我期望一旦在单独的线程上执行添加并且*dynamic-x*应该使用当前线程本地值(我应该是 10),第三种情况将返回 15。但是,出乎我意料的是,它返回了 25。
我哪里错了?
基本上,我想要的是在 ClojureScript 中实现这段代码:
var win = window.open('foo.html', 'windowName');
var timer = setInterval(function() {
if(win.closed) {
clearInterval(timer);
alert('closed');
}
}, 1000);
Run Code Online (Sandbox Code Playgroud)
我试过这个:
(let [popup (.open js/window "foo.html" "windowName")
interval (.setInterval
js/window
(fn []
(when (.-closed popup)
(do
;; 'interval' is undefined at this point
(.clearInterval js/window interval)
(.alert js/window 'closed')))
1000)]
...)
Run Code Online (Sandbox Code Playgroud)
但是 CLJS 编译器给了我一个interval未定义的警告。
有任何想法吗?
假设我有以下代码:
(def m1 (java.util.HashMap.))
(def m2 (java.util.LinkedHashMap.))
(def m3 {})
Run Code Online (Sandbox Code Playgroud)
我需要一个能够检测来自java的地图的函数,例如:
(map java-map? [m1 m2 m3]) ;; => (true true false)
Run Code Online (Sandbox Code Playgroud)
什么开箱即用?
我已经建立了小项目:
project.clj:
(defproject testing-compilation "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]]
;; this is important!
:aot :all)
Run Code Online (Sandbox Code Playgroud)
SRC/core.clj
(ns testing-compilation.core)
(def x (do
(println "Print during compilation?")
1))
Run Code Online (Sandbox Code Playgroud)
然后,当我lein compile在项目目录中,我看到打印输出:
$ lein compile
Compiling testing-compilation.core
Print during compilation?
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么clojure在AOT编译期间评估顶级表单?它们不应该在程序启动时进行评估吗?
作为参考,Common Lisp默认不评估表单,并提供调整此行为的功能.Clojure中有类似的东西吗?如果没有,Clojure文档是否明确说明了这种行为?
UPD:表单也会在启动时进行评估.
指定主命名空间并编写打印的main函数后Hello, world!,我这样做了:
$ lein uberjar
Compiling testing-compilation.core
Print during compilation?
Created testing-compilation-0.1.0-SNAPSHOT.jar
Created testing-compilation-0.1.0-SNAPSHOT-standalone.jar
$ java -jar target/testing-compilation-0.1.0-SNAPSHOT-standalone.jar
Print during compilation?
Hello world!
Run Code Online (Sandbox Code Playgroud) 这是片段:
(defmacro produce-constantly-fn []
(constantly :value))
(defmacro produce-fn []
(fn [& args] :value))
(defn test-fn []
((produce-fn)))
;; during evaluation of form below it throws:
;; java.lang.IllegalArgumentException
;; No matching ctor found for class clojure.core$constantly$fn__4614
(defn test-constantly-fn []
((produce-constantly-fn)))
Run Code Online (Sandbox Code Playgroud)
为什么上一个函数无法编译?该片段可以被视为某种排序宏滥用,但无论如何......
以下表格
(let ((foo nil))
(ecase foo
(:bar 1)
(:baz 2)
(nil 3)))
Run Code Online (Sandbox Code Playgroud)
抛出错误NIL fell through ECASE expression.解决方法似乎是nil用括号括起案例,如下所示:
(let ((foo nil))
(ecase foo
(:bar 1)
(:baz 2)
((nil) 3))) ;; => 3
Run Code Online (Sandbox Code Playgroud)
但为什么第一个例子不起作用?打开的nil案件有一些特殊含义吗?
从这些函数的签名来看,明显的区别在于set-macro-character允许您为单个字符设置reader宏功能,并set-dispatch-macro-character允许您为两个字符的任意组合设置它.这是唯一的区别吗?我什么时候需要使用一个而不是另一个?
clojure ×8
common-lisp ×3
rust ×2
clojure.spec ×1
clos ×1
compilation ×1
dictionary ×1
future ×1
javascript ×1
leiningen ×1
macros ×1
multimethod ×1
reader-macro ×1
sbcl ×1
setinterval ×1
types ×1
wasm-bindgen ×1
webassembly ×1