unifying hash and proc syntax

J

Joel VanderWerf

Hey, crazy idea. Why not use the same syntax for hashes and arrays? For
example:

{
1 => 2,
2 => 3,
3 => 4,
|x|
x + 1
}

This is essentially a hash with a default_proc that adds 1 to its argument.

Leave out the => pairs and you get something that acts like a proc.
Leave out the |...|... construct and you get something that acts like a
hash. With both, it's a hash with a default proc.

It's backwards compatible.

Are there any syntactic ambiguities?
 
H

Hal Fulton

Joel said:
Hey, crazy idea. Why not use the same syntax for hashes and arrays? For
example:

{
1 => 2,
2 => 3,
3 => 4,
|x|
x + 1
}

This is essentially a hash with a default_proc that adds 1 to its argument.

Leave out the => pairs and you get something that acts like a proc.
Leave out the |...|... construct and you get something that acts like a
hash. With both, it's a hash with a default proc.

It's a fllor wax AND a dessert topping!
It's backwards compatible.

Delicious! And just look at that shine!
Are there any syntactic ambiguities?

Hmm, not sure. But it certainly gives me a headache to think about it.


Hal
 
C

Csaba Henk

example:

{
1 => 2,
2 => 3,
3 => 4,
|x|
x + 1
}

This is essentially a hash with a default_proc that adds 1 to its argument.

Witty! What's somewhat dubious in it is melting two things which are so
different as objects. A hash is basicly transparent, a proc is basicly
opaque. And an object which has a proc as an attribute in some sense
won't just become a proc...

But, hm, all-in-all, I like it. I always tend to think of hashes as
mappings defined at finitely many places, abstracting from the data
structure which implements it (and gives it its name).

Csaba
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top