Ali Naqvi is an IT professional with more than 20 years of IT industry experience. He is an avid developer and OSS contributor. His passion includes development, software architecture, DevOps.
Talking about intuitiveness, don't know why Proc is treated differently in Crystal. Other languages treats function pointers as normal functions and one don't need special syntax or invoke special methods for that.
This is the same in Ruby too. The main reason is that you can invoke methods without parentheses. Let's say you have this:
deffoo->{1}end
And suppose we could invoke a Proc just by using parentheses, like a regular method invocation. So it would be like this:
f=foo# this returns the Procf()# this would in theory invoke it
But then there are some ambiguities with the above... The main one is this:
f=foo()# does this return the Proc and executes it, or it just returns the proc?
Another ambiguity is what happens if there's an f method in the same scope:
deff2endf=foof()# Does this call the `f` Proc, or the `f` method?
If we decide it calls the method, then if f didn't exist and it now exists (someone added it after we wrote those two lines), the meaning would change. If we decide it calls the f Proc, then all foo_bar() calls must be treated like assuming they are procs, if there's a foo_bar local variable in the current scope. But maybe once the language was popular they thought about doing that but it would be a huge breaking change. It's very common to have local variables and methods be named the same in Ruby.
In Crystal we made it the same as in Ruby. I actually think having a call method simplifies things: you can be sure what you have is a Proc. Also procs in Crystal aren't used that much...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Talking about intuitiveness, don't know why
Proc
is treated differently in Crystal. Other languages treats function pointers as normal functions and one don't need special syntax or invoke special methods for that.This is the same in Ruby too. The main reason is that you can invoke methods without parentheses. Let's say you have this:
And suppose we could invoke a Proc just by using parentheses, like a regular method invocation. So it would be like this:
But then there are some ambiguities with the above... The main one is this:
Another ambiguity is what happens if there's an
f
method in the same scope:If we decide it calls the method, then if
f
didn't exist and it now exists (someone added it after we wrote those two lines), the meaning would change. If we decide it calls thef
Proc, then allfoo_bar()
calls must be treated like assuming they are procs, if there's afoo_bar
local variable in the current scope. But maybe once the language was popular they thought about doing that but it would be a huge breaking change. It's very common to have local variables and methods be named the same in Ruby.In Crystal we made it the same as in Ruby. I actually think having a
call
method simplifies things: you can be sure what you have is aProc
. Also procs in Crystal aren't used that much...