How correct translate Smalltalk blocks in C#

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
vam
Reply | Threaded
Open this post in threaded view
|

How correct translate Smalltalk blocks in C#

vam
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: How correct translate Smalltalk blocks in C#

Sean Malloy-10
In C# 1.0:

public delegate object NiladicBlock();

static void Main()
{
    print(new NiladicBlock(MyBlock));
}

static void print(NiladicBlock block)
{
    Console.WriteLine(block());
}

static object MyBlock()
{
    return "Hello";
}

Qas you can see, incredibly verbose.

In C# 2.0, slightly less so:


public delegate object NiladicBlock();

static void Main()
{
    print(delegate() {
        return "Hello"; });
}

static void print(NiladicBlock block)
{
    Console.WriteLine(block());
}


still totally shit compared to Smalltalk blocks. However I have used
delegates like this with some success.


For example, you can start to capture some of the enumeration patterns from
Smalltalk in C# 2.0:

class Users : List<User>
{
    public delegate void DoBlock(User user);

    public void Do(DoBlock block)
    {
        foreach (User u in this) {
            block(u); }

    }
}

and calling code:

Users u = new Users();
users.Add(new User("Sean"));
users.Add(new User("Vladimir"));
users.Do(delegate(User u) {
    Console.WriteLine(u.Name); });

It's verbose, and until the C# world get their head around lambda functions
and generics, you may want to avoid writing too much code using it. Until
people start to comprehend it.

Hopefully by then they'll have been led to Ruby, and then start wondering
about this Smalltalk thing they heard about....





"Vladimir Musulainen" <[hidden email]> wrote in message
news:[hidden email]...
> Hello!
>
> How to translate Smalltalk blocks to C# most correctly?
>
> Any hints or solutions are welcome!
> Thank you in advance,
>
> Vladimir Musulainen