ClojureDocs

导航

命名空间

符号

clojure.core

自 1.0 起提供 (源码)
  • (symbol name)
  • (symbol ns name)
Returns a Symbol with the given namespace and name. Arity-1 works
on strings, keywords, and vars.
3 Examples
;; Returns a symbol with the given namespace and name.
;;
;; (symbol name): name can be a string or a symbol.
;;
;; (symbol ns name): ns and name must both be strings.
;;
;; A symbol string begins with a non-numeric character and can contain
;; alphanumeric characters and *, +, !, -, _, and ?.  (see
;; https://clojure.org/reader for details).
;;
;; symbol does not validate input strings for ns and name, and may return
;; improper symbols with undefined behavior for non-conformant ns and
;; name.

user=> (symbol 'foo)
foo

user=> (symbol "foo")
foo

user=> (symbol "clojure.core" "foo")
clojure.core/foo
;; some gotchas to be aware of:

user=> (symbol "user" 'abc)
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)

user=> (symbol *ns* "abc")
ClassCastException clojure.lang.Namespace cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)

user=> (symbol 'user "abc")
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String  clojure.core/symbol (core.clj:523)


;; Warning - the following generated symbols are non-conformant and may wreak
;; serious havoc in the near/far future when least expected...

user=> (symbol "abc def")
abc def

user=> (symbol "123def")
123def

user=> (symbol "/123/def/ghi")
/123/def/ghi

user=> (symbol "/abc/def/ghi")
/abc/def/ghi
;; but keywords and numbers are not names
(symbol 3)
;; Long cannot be cast to String

;; ... and so they cannot be converted to symbols
(symbol :dog) 
;; Keyword cannot be cast to String
See Also

The symbol must resolve to a var, and the Var object itself (not its value) is returned. The reader ...

Added by boxie

Returns true if v is of type clojure.lang.Var

Added by boxie

Return true if x is a Symbol

Added by franks42

Returns the name String of a string, symbol or keyword.

Added by franks42

Returns the namespace String of a symbol or keyword, or nil if not present.

Added by franks42

Returns a Keyword with the given namespace and name. Do not use : in the keyword strings, it will...

Added by gstamp

same as (ns-resolve *ns* symbol) or (ns-resolve *ns* &env symbol)

Added by blx

Also reader macro: @ref/@agent/@var/@atom/@delay/@future/@promise. Within a transaction, returns t...

Added by MicahElliott
0 Notes
No notes for symbol