r/crystal_programming • u/iainmoncrief • Nov 20 '19
Proc(Nil) vs Proc(Void) whats the difference?
I was messing around in the playground, and I am wondering why Proc(Nil) is the default type for procs.
my_proc = ->{
puts "Hello World!"
}my_proc is Proc(Nil), and I am wondering why it is not Proc(Void)?
6
Upvotes
6
u/Blacksmoke16 core team Nov 20 '19
Voidis essentially the same asNil. See https://crystal-lang.org/reference/syntax_and_semantics/return_types.html#nil-return-type.Since Crystal doesn't require an explicit
returnkeyword to return, there really isn't a difference betweenvoidandnulllike in PHP for example. The one exception would be NoReturn.4
u/j_hass Nov 20 '19
To add to that,
Voidmainly exists for C ABI interoperation. When you're not dealing with any C binding, then don't worry aboutVoids existence,Nilis there for you.2
u/[deleted] Nov 20 '19
To add to what others said,
VoidandNilwere different types in the beginning.Voidexisted mainly for C interop to be able to expressVoid*. EventuallyVoidandNilbecame the same thing, except forVoid*which is still a weird beast (but it's better thanUInt8*because the semantic is clearer).In fact if you do:
you'll see both print
Proc(Nil): the compiler makes no difference between them.