Hemant Gautam <gettingerr...@gmail.com> writes:

Hi Hemant,

> This is the code
>
> (ns clojure_begins_19_08_2014.core)

Use hyphens instead of underscores for namespace and var names.

> (defn first-fn[x]
>   (print x))
>
> (defn first-fn[x]
>   (print "Value of x with String " x))
>
> (first-fn 10)
>
> See, above code consists of two function with same name and with same
> number of arguments.  When I call first-fn, the function declared
> later gets called. I get output as:
>
> Value of x with String  10
> nil
>
> So can any one explain how this thing is happening

The second definition overrides the first one.  That's possible in
Clojure because when developing you usually have a REPL connection and
add and change functions in the live system as you go.

> and also explain how we can have same function written 2 times in a
> same namespace.

You can't.  Or more precise, you can't after the file has been loaded.
Note that when your file above is loaded, in between of the two defns
you could call (first-fn 10) which would only print "10" because in this
short region, the first version is active.

Possibly one could enhance the Clojure compiler or reader so that it
emits a warning in case a function is defined twice in the same file and
namespace.  But since you can also have multiple files defining stuff in
the same namespace, I guess intended overriding during a REPL session
and unintended overriding like above may become hard to distinguish.

Bye,
Tassilo

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to