I’m currently working on an F# library that will be accessed from C#. When doing this the obvious question is; “How will this look from C#?” This wasn't quite clear to me at first so I’ll go through a couple custom F# types and show how they are compiled.
Record type, Fermion.fs:
type Spin = | Up = 0 | Down = 1 type Class = | Quark = 0 | Lepton = 1 type Type = ... | Electron = 6 ...
type Fermion = { spin: Spin; particleClass: Class; particleType: Type } member yada.Spin = yada.spin member v.Class = v.particleClass member v.Type = v.particleType member stuff.GetDescription() = System.String.Format( "This {0} is a spin {1} {2}.", stuff.particleType.ToString(), stuff.spin.ToString(), stuff.particleClass.ToString())
Using the record type in F#
let electron = { spin = Spin.Down; particleClass = Class.Lepton; particleType = Type.Electron }
In Reflector:
public static class Fermion { public sealed class Fermion : IStructuralEquatable, IComparable, IStructuralComparable { internal Particles.Class particleClass@; internal Particles.Type particleType@; internal Particles.Spin spin@; public Fermion( Particles.Spin spin, Particles.Class particleClass, Particles.Type particleType) { this.spin@ = spin; this.particleClass@ = particleClass; this.particleType@ = particleType; } public string GetDescription() { return string.Format("This {0} is a spin {1} {2}.", this.particleType@.ToString(), this.spin@.ToString(), this.particleClass@.ToString()); } public Particles.Class Class { get { return this.particleClass@; } } public Particles.Class particleClass { get { return this.particleClass@; } } public Particles.Type particleType { get { return this.particleType@; } } public Particles.Spin spin { get { return this.spin@; } } public Particles.Spin Spin { get { return this.spin@; } } public Particles.Type Type { get { return this.particleType@; } } public int CompareTo(Particles.Fermion obj); public sealed override int CompareTo(object obj); public sealed override int CompareTo(object obj, IComparer comp); public bool Equals(Particles.Fermion obj); public sealed override bool Equals(object obj); public sealed override bool Equals(object obj, IEqualityComparer comp); public override int GetHashCode(); public sealed override int GetHashCode(IEqualityComparer comp); } }
Some things to note:
Now lets mix things up a bit…
Constructed type, Fermion.fs:
namespace Particles type Spin = | Up = 0 | Down = 1 type Class = | Quark = 0 | Lepton = 1 type Type = ... | Electron = 6 ...
type Fermion(spin: Spin, particleClass: Class, particleType: Type) = let _description = System.String.Format( "This {0} is a spin {1} {2}.", particleType.ToString(), spin.ToString(), particleClass.ToString()) member yada.Spin = spin member v.Class = particleClass member v.Type = particleType member stuff.GetDescription() = _description
Using the constructed type in F#:
let electron = new Fermion(Spin.Down, Class.Lepton, Type.Electron)
public class Fermion { internal string _description; internal Class particleClass; internal Type particleType; internal Spin spin; public Fermion(Spin spin, Class particleClass, Type particleType) { this.spin = spin; this.particleClass = particleClass; this.particleType = particleType; this._description = string.Format("This {0} is a spin {1} {2}.", this.particleType.ToString(), this.spin.ToString(), this.particleClass.ToString()); } public string GetDescription() { return this._description; } public Class Class { get { return this.particleClass; } } public Spin Spin { get { return this.spin; } } public Type Type { get { return this.particleType; } } }
Remember Me
a@href@title, b, i, strike, strong, u