0.0.1 • Published 10 years ago

algebraic-data-traits v0.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
10 years ago

Writing Java in Javascript considered harmful

I made this because I am tired of seeing this:

function NotReallyAClass() {
	this._notReallyPrivate = 'bar';
	return this
}

NotReallyAClass.prototype = new SomeGodClass();

NotReallyAClass.prototype.crash = function () {
	...
}

Stop it!

Usage

npm install -g sweet.js
npm install jmars/algebraic-data-traits
sjs -m adt/macros -m sparkler/macros -o index.js index.sjs

Dependencies

trait Speaks {
	log(str) {
		console.log(str)
	}
}

trait Barks {
	requires log

	bark() {
		this.log('WOOF!')
	}
}

data Animal {
	Dog {
		name: String
	},
	Cat {
		name: String
	}
}

impl Dog {
	mixin Speaks
	mixin Barks
}

impl Cat {
	requires log

	mixin Speaks
	
	meow() {
		this.log('MEOW!')
	}
}

var fluffy = Cat.create({name:'fluffy'});
fluffy.meow() // MEOW!

// And sparkler.js compatible
fluffy match {
	case Cat{name} => console.log(name) // fluffy
}