vivri / adjective Goto Github PK
View Code? Open in Web Editor NEWProgramming is an exercise in linguistics; spice-up Scala types with Adjective.
License: MIT License
Programming is an exercise in linguistics; spice-up Scala types with Adjective.
License: MIT License
From the running spec:
// First, we define the precise types that make up our domain/universe/ontology
object PersonOntology {
// `Adjective[T]` is the building block of our type algebra
// Try to make them as atomic as possible
case object DbId extends Adjective[Int] ((id)=> 0 <= id && id < 2000000)
case object NameSequence extends Adjective[String] (_.matches("^[A-Z][a-zA-Z]{1,31}$"))
case object DisallowedSequences extends Adjective[String] (_.toLowerCase.contains("fbomb"))
case object ScottishLastName extends Adjective[String] (_ startsWith "Mc")
case object JewishLastName extends Adjective[String] (_ endsWith "berg")
// We use boolean algebra to combine base adjectives into more nuanced adjectives
val LegalName = NameSequence & ~DisallowedSequences // `~X` negates `X`
val FirstName = LegalName
val SomeHeritageLastName = LegalName & (ScottishLastName <+> JewishLastName) // `<+>` stands for Xor, ⊕ is the math notation
}
// < ... Irrelevant things go here >
// Trying to precisely type the Includes/Excludes exposes a
// little bit of clunkiness in the path-dependent types of `val`s
validPerson shouldBe Right(
Person(
Includes(DbId,123), // this works great because DbId is a type, not a `val`
Includes(FirstName, "Bilbo").asInstanceOf[FirstName.^], // ouch!
Includes(SomeHeritageLastName, "McBeggins").asInstanceOf[SomeHeritageLastName.^])) // one more ouch.
This is very clunky, and detracts from both the usability and safety of using the library.
I believe it is caused by the fact that Scala treats static and dynamic type
declarations differently, with the dynamically-declared type
being a second-rate citizen.
We should create a mechanism whereby this wart is either hidden behind some library interface, or avoided altogether.
As a follow-up to #3 , automated CI pipeline would be a great addition. Using sbt-ci-release, the entire release & testing process can be nicely automated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.