简体   繁体   English

Clojure中具有相同名称的多个功能

[英]multiple functions with same name in clojure

(defn hi[](+ 5 6))  
(hi)  
(defn hi[](+ 6 7))  
(hi) 

Hi, i'm new to clojure . 嗨,我是clojure的新手。 as above writen code i wrote two functions with same name. 如上所写的代码,我写了两个同名函数。 can we have same name for multiple functions in clojure. 我们可以为Clojure中的多个功能使用相同的名称吗? if yes does it conflict within same namespaces? 如果是,是否在相同的命名空间内发生冲突?

You cannot have several functions with the same name in the same namespace. 在同一个名称空间中,不能有多个具有相同名称的函数。 If you define function foo and then redefine it again, newer version will replace the older. 如果您定义函数foo ,然后再次重新定义它,则较新的版本将替换较旧的版本。

It's not quite clear what you're trying to achieve, but Clojure has quite a few ways to have several implementations under one name. 目前尚不清楚您要实现的目标,但是Clojure有很多方法可以用一个名称实现多个实现。

Arity Dispatch 阿里派遣

You can dispatch functions on arity: 您可以在arity上调度函数:

(defn foo
  ([x] :version-one)
  ([x y] :version-two)
  ([x y & more] :version-tree))

Different arities of the same function can call one another, and this is somewhat equivalent to having several functions with the same name. 同一函数的不同区域可以互相调用,这在某种程度上等同于具有相同名称的多个函数。

Multimethods 多种方法

You can dispatch using multimethods. 您可以使用多种方法进行调度。 They allow you to define a function that will be called on arguments and its result will be used for dispatching. 它们允许您定义一个将在参数上调用的函数,其结果将用于调度。

(defmulti foo odd?)

(defmethod foo true [x]
  (format "look, ma, %d is odd!" x))

(defmethod foo false [x]
  (format "look, ma, %d is even!" x))

Protocols 通讯协定

This technique is similar to polymorphism in other languages. 此技术类似于其他语言中的多态。 You can dispatch on argument type. 您可以调度参数类型。 Here is example: 这是示例:

(defprotocol foo
  (my-fnc [x] "description..."))

(extend-protocol foo
  Number
  (my-fnc [x] :number-version)
  String
  (my-fnc [x] :string-version))

Multimethods are most powerful tool, you can emulate protocols with them this way: 多重方法是最强大的工具,您可以通过以下方式使用它们来仿真协议:

(defmulti foo class)

I wrote two functions with same name. 我用相同的名字写了两个函数。

Yes and No: 是和否:

  • You wrote two function definitions with the same name. 您用相同的名称编写了两个函数定义。
  • The second definition overwrote the first. 第二个定义覆盖了第一个。
  • The functions never shared the name. 这些函数从不共享名称。

Can we have same name for multiple functions in clojure? Clojure中的多个功能可以使用相同的名称吗?

Not in the same namespace. 不在同一名称空间中。 A second def or defn for a symbol erases the first. 符号的第二个defdefn会删除第一个。

  • There is only one symbol with a given name in a namespace. 名称空间中只有一个具有给定名称的符号。
  • A symbol refers to only one var at a time. 一个符号一次仅指一个var
  • The def form ties a symbol to the var it refers to. def形式将符号与其引用的var在一起。
  • The fn form creates a function. fn表单创建一个函数。
  • The defn macro is a wrapper round the def and fn forms, causing the named symbol to refer to a var holding the function. defn宏是deffn形式的包装,导致命名符号引用包含该函数的var
  • Symbols with the same name in different namespaces do not conflict: they are different symbols. 在不同名称空间中具有相同名称的符号不会冲突:它们是不同的符号。

If yes does it conflict within the same namespace? 如果是,在同一名称空间内是否发生冲突?

Yes. 是。

But ... 但是...

One function can have several arities - argument counts. 一个函数可以具有多个变量-参数计数。 This applies to functions referred to by symbols through vars and in other contexts. 这适用于符号通过var和其他上下文引用的函数。


Function definition is a side-effect of evaluating a defn . 函数定义是评估的一个副作用 defn The best explanation I know of how lisps evaluate function definitions is Chapter 2 of Paul Graham's On Lisp . 我所知道的关于lisps如何评估函数定义的最好解释是Paul Graham的On Lisp第2章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM