immutability - Can I call this C# class "immutable"? -



immutability - Can I call this C# class "immutable"? -

i need create mutable class immutable, looks following. however, still i'm not sure have "immutable* class, , if is, kind of immutability called ?

public class b<c, m> c : icomparable<c> m : imetadata { internal b(char tau, m metadata, b<c, m> nextblock) { if (tau == 'r') omega = 1; _lambda = new list<lambda<c, m>>(); _lambda.add(new lambda<c, m>(tau: tau, ati: metadata)); foreach (var item in nextblock.lambda) if (item.tau != 'l') _lambda.add(new lambda<c, m>(tau: 'm', ati: item.ati)); } internal int omega { private set; get; } private list<lambda<c, m>> _lambda { set; get; } internal readonlycollection<lambda<c, m>> lambda { { homecoming _lambda.asreadonly(); } } internal b<c, m> update(int omega, char tau, m metadata) { b<c, m> newblock = new b<c, m>(); newblock.omega = omega; newblock._lambda = new list<lambda<c, m>>(this._lambda); newblock._lambda.add(new lambda<c, m>(tau: tau, ati: metadata)); homecoming newblock; } internal b<c, m> update(dictionary<uint, lambda<c, m>> lambdas) { b<c, m> newblock = new b<c, m>(); newblock.omega = this.omega; newblock._lambda = new list<lambda<c, m>>(); foreach (var l in lambdas) newblock._lambda.add(new lambda<c, m>(tau: l.value.tau, ati: l.value.ati)); homecoming newblock; } } public class lambda<c, m> c : icomparable<c> m : imetadata { internal lambda(char tau, m ati) { this.tau = tau; this.ati = ati; } internal char tau { private set; get; } internal m ati { private set; get; } }

based on application b needs changed time time; hence maintain property of immutability, every update needs done throught update function returns totally new b.

update

to shed lite on imetadata cleverly spotted jon skeet please consider next definitions:

public interface imetadata { uint32 hashkey { set; get; } }

and next class passed m b<c, m>

public class metadata : imetadata { public uint32 hashkey { set; get; } }

no external code can ever observe mutations of type, plenty considered "immutable" in mutual speach. times ever mutated during it's own constructor; never mutated 1 time created, no external entity ever observe mutation of type. tends true of types considered "immutable".

while type have technical ability mutate outside of constructor in has both fields not readonly mutable list, never performs such mutations or exposes means of mutating data.

c# immutability mutable

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

SQL Server : need assitance parsing delimted data and returning a long concatenated string -