Re: NullPointer when just returning nil?

2008-11-29 Thread Rich Hickey


On Nov 29, 2008, at 9:06 AM, Stephen C. Gilardi wrote:

>
>
> On Nov 29, 2008, at 8:03 AM, Ralf Bensmann wrote:
>
>> But #(...) and (fn [] ...) should be the same?
>
> The other replies have noted why that's not right.
>
> There is a way to get the effect you were looking for. If you want to
> return a constant or do something with side-effects before returning a
> value, you can use do:
>
> #(do nil) is a function that will always return nil
>
> #(do (prn "hi") 5) will print "hi" and then return 5
>


I really dislike the use of do other than for side effects. I imagine  
a code checker which will eventually flag usage of do et al.

Someone did show (fn [] x). If it's somehow important to use #(),  
please use:

#(identity x)


Rich

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Ralf Bensmann
I think the NullPointerException was misleading for me... a more
informational error message should be added.
Rich, what do you think?

On Sat, Nov 29, 2008 at 3:41 PM, Ralf Bensmann
<[EMAIL PROTECTED]>wrote:

> Argl... just forgot one of the "basic rules" ;)
>
>
> On Sat, Nov 29, 2008 at 3:33 PM, Randall R Schulz <[EMAIL PROTECTED]>wrote:
>
>>
>> On Saturday 29 November 2008 06:27, Ralf Bensmann wrote:
>> > Thanks for clarification. But I am wondering about a function can
>> > return nil:
>> >
>> > user=> (fn [] ((+ 1 2) nil))
>> > #
>> >
>> > But "just returning nil" is not ok?
>> > user=> (fn [] (nil))
>>
>> You're not returning nil, you're trying to apply nil (as if it were a
>> function) to an empty argument list.
>>
>>
>> > java.lang.NullPointerException (NO_SOURCE_FILE:24)
>> > user=> (fn [] ((nil)))
>> > java.lang.NullPointerException (NO_SOURCE_FILE:25)
>>
>> Now you're trying to apply the result of applying nil to an empty
>> argument list to an empty argument list. Naturally, it fails at the
>> same point, which is the inner attempt.
>>
>>
>> It's just simpler than you're trying to make it:
>>
>> user=> ((fn [] nil))
>> nil
>>
>>
>>
>> > Thanks
>> > -Ralf
>>
>>
>> Randall Schulz
>>
>> >>
>>
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Ralf Bensmann
Argl... just forgot one of the "basic rules" ;)


On Sat, Nov 29, 2008 at 3:33 PM, Randall R Schulz <[EMAIL PROTECTED]> wrote:

>
> On Saturday 29 November 2008 06:27, Ralf Bensmann wrote:
> > Thanks for clarification. But I am wondering about a function can
> > return nil:
> >
> > user=> (fn [] ((+ 1 2) nil))
> > #
> >
> > But "just returning nil" is not ok?
> > user=> (fn [] (nil))
>
> You're not returning nil, you're trying to apply nil (as if it were a
> function) to an empty argument list.
>
>
> > java.lang.NullPointerException (NO_SOURCE_FILE:24)
> > user=> (fn [] ((nil)))
> > java.lang.NullPointerException (NO_SOURCE_FILE:25)
>
> Now you're trying to apply the result of applying nil to an empty
> argument list to an empty argument list. Naturally, it fails at the
> same point, which is the inner attempt.
>
>
> It's just simpler than you're trying to make it:
>
> user=> ((fn [] nil))
> nil
>
>
>
> > Thanks
> > -Ralf
>
>
> Randall Schulz
>
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Randall R Schulz

On Saturday 29 November 2008 06:27, Ralf Bensmann wrote:
> Thanks for clarification. But I am wondering about a function can
> return nil:
>
> user=> (fn [] ((+ 1 2) nil))
> #
>
> But "just returning nil" is not ok?
> user=> (fn [] (nil))

You're not returning nil, you're trying to apply nil (as if it were a 
function) to an empty argument list.


> java.lang.NullPointerException (NO_SOURCE_FILE:24)
> user=> (fn [] ((nil)))
> java.lang.NullPointerException (NO_SOURCE_FILE:25)

Now you're trying to apply the result of applying nil to an empty 
argument list to an empty argument list. Naturally, it fails at the 
same point, which is the inner attempt.


It's just simpler than you're trying to make it:

user=> ((fn [] nil))
nil



> Thanks
> -Ralf


Randall Schulz

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Ralf Bensmann
Thanks for clarification. But I am wondering about a function can return
nil:

user=> (fn [] ((+ 1 2) nil))
#

But "just returning nil" is not ok?
user=> (fn [] (nil))
java.lang.NullPointerException (NO_SOURCE_FILE:24)
user=> (fn [] ((nil)))
java.lang.NullPointerException (NO_SOURCE_FILE:25)

Thanks
-Ralf

On Sat, Nov 29, 2008 at 3:18 PM, Rich Hickey <[EMAIL PROTECTED]> wrote:

>
>
>
> On Nov 29, 9:06 am, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
> > On Nov 29, 2008, at 8:03 AM, Ralf Bensmann wrote:
> >
> > > But #(...) and (fn [] ...) should be the same?
> >
> > The other replies have noted why that's not right.
> >
> > There is a way to get the effect you were looking for. If you want to
> > return a constant or do something with side-effects before returning a
> > value, you can use do:
> >
> > #(do nil) is a function that will always return nil
> >
> > #(do (prn "hi") 5) will print "hi" and then return 5
> >
>
> I really dislike the use of do other than for side effects. I imagine
> a code checker which will eventually flag usage of do et al.
>
> Someone did show (fn [] nil). If it's somehow important to use #(),
> please use:
>
> #(identity nil)
>
> Rich
>
> >
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Rich Hickey



On Nov 29, 9:06 am, "Stephen C. Gilardi" <[EMAIL PROTECTED]> wrote:
> On Nov 29, 2008, at 8:03 AM, Ralf Bensmann wrote:
>
> > But #(...) and (fn [] ...) should be the same?
>
> The other replies have noted why that's not right.
>
> There is a way to get the effect you were looking for. If you want to
> return a constant or do something with side-effects before returning a
> value, you can use do:
>
> #(do nil) is a function that will always return nil
>
> #(do (prn "hi") 5) will print "hi" and then return 5
>

I really dislike the use of do other than for side effects. I imagine
a code checker which will eventually flag usage of do et al.

Someone did show (fn [] nil). If it's somehow important to use #(),
please use:

#(identity nil)

Rich

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Stephen C. Gilardi


On Nov 29, 2008, at 8:03 AM, Ralf Bensmann wrote:

> But #(...) and (fn [] ...) should be the same?

The other replies have noted why that's not right.

There is a way to get the effect you were looking for. If you want to  
return a constant or do something with side-effects before returning a  
value, you can use do:

#(do nil) is a function that will always return nil

#(do (prn "hi") 5) will print "hi" and then return 5

--Steve


--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Rich Hickey


On Nov 29, 2008, at 8:03 AM, Ralf Bensmann wrote:

> But #(...) and (fn [] ...) should be the same?

No, as stated here:

http://clojure.org/reader

The anonymous fn reader macro expands as follows:

#(...) => (fn [args] (...))

If you think about it a bit:

#(+ 2 %) => (fn [x] (+ 2 x))

Note the parens remain around the call.

If #(42) were to become (fn [] 42), then #(+ 2 %) would become (fn [x]  
+ 2 x), no parens, no call to +.

Rich


>
>
> On Sat, Nov 29, 2008 at 1:48 PM, Parth Malwankar <[EMAIL PROTECTED] 
> > wrote:
>
> On Nov 29, 5:29 pm, "Ralf Bensmann" <[EMAIL PROTECTED]>
> wrote:
> > Hi,
> >
> > is this the intended behavior?
> >
> > user=> #(nil)
> > java.lang.NullPointerException (NO_SOURCE_FILE:12)
> > user=> (def b #(nil))
> > java.lang.NullPointerException (NO_SOURCE_FILE:13)
> >
>
> This is expected.
> #(nil) is the same as (fn [] (nil)) and hence the failure.
> (fn [] nil) is what you want.
>
> > This works:
> > user=> #('nil)
> > #
> >
>
> I am not very clear on whats happening here to comment.
>
> Parth
>
> > Thanks,
> > -Ralf
>
>
> >


--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Parth Malwankar



On Nov 29, 6:03 pm, "Ralf Bensmann" <[EMAIL PROTECTED]>
wrote:
> But #(...) and (fn [] ...) should be the same?

I suppose that might make the syntax somewhat ugly.
E.g.:
(fn [x] (foo x)) would become #((foo %)).

This particular syntax is a frequent point of confusion but
then once you get it its cleaner. The current syntax fits the
common case better where you might do #(foo %1 %2) sort
of a thing.

I suppose once an FAQ for Clojure comes up this goes there :)

Parth


>
> On Sat, Nov 29, 2008 at 1:48 PM, Parth Malwankar
> <[EMAIL PROTECTED]>wrote:
>
>
>
> > On Nov 29, 5:29 pm, "Ralf Bensmann" <[EMAIL PROTECTED]>
> > wrote:
> > > Hi,
>
> > > is this the intended behavior?
>
> > > user=> #(nil)
> > > java.lang.NullPointerException (NO_SOURCE_FILE:12)
> > > user=> (def b #(nil))
> > > java.lang.NullPointerException (NO_SOURCE_FILE:13)
>
> > This is expected.
> > #(nil) is the same as (fn [] (nil)) and hence the failure.
> > (fn [] nil) is what you want.
>
> > > This works:
> > > user=> #('nil)
> > > #
>
> > I am not very clear on whats happening here to comment.
>
> > Parth
>
> > > Thanks,
> > > -Ralf
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Ralf Bensmann
But #(...) and (fn [] ...) should be the same?

On Sat, Nov 29, 2008 at 1:48 PM, Parth Malwankar
<[EMAIL PROTECTED]>wrote:

>
> On Nov 29, 5:29 pm, "Ralf Bensmann" <[EMAIL PROTECTED]>
> wrote:
> > Hi,
> >
> > is this the intended behavior?
> >
> > user=> #(nil)
> > java.lang.NullPointerException (NO_SOURCE_FILE:12)
> > user=> (def b #(nil))
> > java.lang.NullPointerException (NO_SOURCE_FILE:13)
> >
>
> This is expected.
> #(nil) is the same as (fn [] (nil)) and hence the failure.
> (fn [] nil) is what you want.
>
> > This works:
> > user=> #('nil)
> > #
> >
>
> I am not very clear on whats happening here to comment.
>
> Parth
>
> > Thanks,
> > -Ralf
>

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



Re: NullPointer when just returning nil?

2008-11-29 Thread Parth Malwankar



On Nov 29, 5:29 pm, "Ralf Bensmann" <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> is this the intended behavior?
>
> user=> #(nil)
> java.lang.NullPointerException (NO_SOURCE_FILE:12)
> user=> (def b #(nil))
> java.lang.NullPointerException (NO_SOURCE_FILE:13)
>

This is expected.
#(nil) is the same as (fn [] (nil)) and hence the failure.
(fn [] nil) is what you want.

> This works:
> user=> #('nil)
> #
>

I am not very clear on whats happening here to comment.

Parth

> Thanks,
> -Ralf
--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---



NullPointer when just returning nil?

2008-11-29 Thread Ralf Bensmann
Hi,

is this the intended behavior?

user=> #(nil)
java.lang.NullPointerException (NO_SOURCE_FILE:12)
user=> (def b #(nil))
java.lang.NullPointerException (NO_SOURCE_FILE:13)

This works:
user=> #('nil)
#

Thanks,
-Ralf

--~--~-~--~~~---~--~~
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
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~--~~~~--~~--~--~---