none
Newbie question about type RRS feed

  • Question

  • Hi,

    I have downloaded Don Syme tutorials and started browsing the code.

    I am puzzled with the following declaration:
    type Card = Card of Rank * Suit

    I understand that Rank * Suit is a tuple, but the recursive nature of the declaration confused me.

    How this translates in C#?
    (I am starting with F#. Sorry if the question is stupid ...)

    Thanks in advance
    Philippe

    Thursday, December 20, 2012 8:04 AM

Answers

  • The name Card on the right hand side is label, that just happens to have the same name as the type, as can be seen in the definition of Card2
    type Rank = RankLabel
    type Suit = SuitLabel
    
    type Card = Card of Rank * Suit 
    type Card2 = Label of Rank * Suit 
    Building this in a module named Module1, and then using a decompiler to get the C# equivalent we see (dropping all the attribute decorations and the interface implementations) the Card2 type is implemented as
    public class Card2 : IEquatable<Module1.Card2>, IStructuralEquatable, IComparable<Module1.Card2>, IComparable, IStructuralComparable
    {
    	internal readonly Module1.Rank item1;
    	internal readonly Module1.Suit item2;
    
    	public int Tag 	{get{return 0;}}
    	public Module1.Rank Item1 {get{return this.item1;}}
    	public Module1.Suit Item2 {get{return this.item2;}}
    	public static Module1.Card2 NewLabel(Module1.Rank item1, Module1.Suit item2)
    	{
    		return new Module1.Card2(item1, item2);
    	}
    	internal Card2(Module1.Rank item1, Module1.Suit item2)
    	{
    		this.item1 = item1;
    		this.item2 = item2;
    	}
    	internal object __DebugDisplay()...
    	public sealed override int CompareTo(Module1.Card2 obj)...
    	public sealed override int CompareTo(object obj)...
    	public sealed override int CompareTo(object obj, IComparer comp)...
    	public sealed override int GetHashCode(IEqualityComparer comp)...
    	public sealed override int GetHashCode()...
    	public sealed override bool Equals(object obj, IEqualityComparer comp)...
    	public sealed override bool Equals(Module1.Card2 obj)...
    	public sealed override bool Equals(object obj)...
    }
    where the Label name on the right hand side of the type definition appears in the form of the static NewLabel factory method.

    Thursday, December 20, 2012 9:20 AM

All replies

  • The name Card on the right hand side is label, that just happens to have the same name as the type, as can be seen in the definition of Card2
    type Rank = RankLabel
    type Suit = SuitLabel
    
    type Card = Card of Rank * Suit 
    type Card2 = Label of Rank * Suit 
    Building this in a module named Module1, and then using a decompiler to get the C# equivalent we see (dropping all the attribute decorations and the interface implementations) the Card2 type is implemented as
    public class Card2 : IEquatable<Module1.Card2>, IStructuralEquatable, IComparable<Module1.Card2>, IComparable, IStructuralComparable
    {
    	internal readonly Module1.Rank item1;
    	internal readonly Module1.Suit item2;
    
    	public int Tag 	{get{return 0;}}
    	public Module1.Rank Item1 {get{return this.item1;}}
    	public Module1.Suit Item2 {get{return this.item2;}}
    	public static Module1.Card2 NewLabel(Module1.Rank item1, Module1.Suit item2)
    	{
    		return new Module1.Card2(item1, item2);
    	}
    	internal Card2(Module1.Rank item1, Module1.Suit item2)
    	{
    		this.item1 = item1;
    		this.item2 = item2;
    	}
    	internal object __DebugDisplay()...
    	public sealed override int CompareTo(Module1.Card2 obj)...
    	public sealed override int CompareTo(object obj)...
    	public sealed override int CompareTo(object obj, IComparer comp)...
    	public sealed override int GetHashCode(IEqualityComparer comp)...
    	public sealed override int GetHashCode()...
    	public sealed override bool Equals(object obj, IEqualityComparer comp)...
    	public sealed override bool Equals(Module1.Card2 obj)...
    	public sealed override bool Equals(object obj)...
    }
    where the Label name on the right hand side of the type definition appears in the form of the static NewLabel factory method.

    Thursday, December 20, 2012 9:20 AM
  • Great answer, Mr Tines. Thanks a lot.
    Thursday, December 20, 2012 3:43 PM
  • Great answer, Mr Tines. Thanks a lot.

    Please take care while marking any post as Answerd.This this post Answer or post given by Mr Tines ?

    One good question is equivalent to ten best answers.

    Thursday, December 20, 2012 3:46 PM