• Advertisement
Sign in to follow this  

[.net] Implementing variants

This topic is 4404 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im working on my latest programming language and trying to implement variants. The language compiles to C# and from there is compiled with codedom. I am stuck on how to implement variants! The language Boo did (Boo calls them duck type) so im sure its possible, if someone could please help I would be very thankful! Ive tried using the object datatype but that doesnt really work at runtime and save input into the variable...cant tell what type it is. So please help!

Share this post


Link to post
Share on other sites
Advertisement
I don't see what duck typing has to do with implementing variants/algebraic types anyways if the code is going to be compiled to .NET/C# then you'll have to (ab)use OO and use the composite pattern to simulate algebraic types.

E.g the equivalent of:

data Tree a = Leaf a | Branch (Tree a) (Tree a)


in C# is roughly:


abstract class Tree<T>{}

sealed class Leaf<Tp> : Tree<Tp> {

Tp val;

public Leaf(Tp val)
{
this.val = val;
}
}

sealed class Branch<Tp> : Tree<Tp>
{

Tree<Tp> left, right;

public Branch(Tree<Tp> lhs, Tree<Tp> rhs)
{
left = lhs;
righ = rhs;
}
}


[Edited by - snk_kid on January 2, 2006 6:32:41 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement