Immutable

stdtraits~ Immutable

Source:

This is a flag trait that in­dic­ates whether a type is im­mut­able.

const as­sert = re­quire('assert');
const { Im­mut­able, isIm­mut­able, typeIs­Im­mut­able, type } = re­quire('fer­rum');

// Mark a cus­tom type as im­mut­able
class Foo {
  [Im­mut­able.sym]() {}
};

as­sert(isIm­mut­able(new Foo()));
as­sert(typeIs­Im­mut­able(Foo));

// Or al­tern­at­ively
class Bar {};
Im­mut­able.impl(Bar, true);

as­sert(isIm­mut­able(new Bar()));
as­sert(typeIs­Im­mut­able(Bar));

// Mark a cus­tom value as im­mut­able
const baz = {};
Im­mut­able.im­pl­Static(baz, true);

as­sert(isIm­mut­able(baz));
as­sert(!typeIs­Im­mut­able(type(baz)));

// Any other classes will not be con­sidered im­mut­able by de­fault
class Bang {};
as­sert(!isIm­mut­able(new Bang()));
as­sert(!typeIs­Im­mut­able(Bang));

Since javas­cript has not real way to en­force ab­so­lute im­mut­ab­il­ity this trait con­siders any­thing im­mut­able that is hard to mutate or really not sup­posed to be mutated. Func­tion is con­sidered im­mut­able des­pite it be­ing pos­sible to as­sign para­met­ers to func­tions...

This is used in a couple paces; spe­cific­ally it is used as a list of types that should be left alone in deep­clone and shal­low­clone.

See: isIm­mut­able See: typeIs­Im­mut­able

By de­fault im­ple­men­ted for: String, Num­ber, Boolean, Reg­Exp, Date, Sym­bol, Func­tion, null, un­defined