Leon K
12/06/2019, 4:51 PMtypealias Person =
{ name :: String
, age :: Int
, mobileNumber :: String
, landlineNumber :: String
, gender :: Gender
}
then this would not contain any information about the mutability of any of the fields.
this is where the rust-inspiration comes in:
Record-mutability would be defined on a per-usage basis.
if you create a person (simplified here):
val person: Person = { name: "Tom", age: 12, gender: Gender.Male } //other fields left out
then this object is deeply immutable. this could be enforced by saying that a record can only store other Record-types or "primitive" types (and maybe a basic set of collections which are guaranteed to be immutable here)
if you wanted to have a mutable version of that person, you would do something like this:
val person: mut#Person = mut#{ name: "Tom", age: 12, gender: Gender.Male } //other fields left out
(this demo-syntax is ugly, i know)
this would mean that all fields of that record (and all fields of potential records stored within it) are mutable, and thus provide a setter (callable via normal =
functions)
turning a mutable record into an immutable one would need to be an explicit operation, because mutable records would be stored like normal classes (by reference), while immutable records would be stored and passed by value, thus be cloned wherever they are passed to.
this approach would have a lot of benefits as well:
- usually, you, the caller, know, if you need something to be mutable. thats why there are loads of classes that have mutable and immutable variants (List
/ MutableList
, etc). this would make it possible to let YOU decide about the mutability your object needs
- functions could guarantee purity in regards to the record by not requiring your arguments to be mutable, thus not being able to mutate them
- you wouldn't need to create builder-classes, because you could just create a mutable version of the record, build your stuff, and then store it as immutable in the end
- as a result of guaranteed deep immutability, the compiler could do heavy optimizations which are currently not possible.
- you could still have mutating methods on the records, by just defining the extension-functions only for mutable versionsLeon K
12/06/2019, 4:58 PMfun addToPhoneBook(info: { name :: String, landLine :: String }) {
// add the person to a phoneBook
}
// in main
val person: Person = { name: "Tom", age: 12, landLine: "142522152334", gender: Gender.Male }
addToPhoneBook(person)
extension functions would look like this:
fun <T: { gender :: Gender }> List<T>.filterIsFemale(): List<T> {
return this.filter { it.gender == Gender.Female }
}
fun <T: { age :: Int }> T.isMinor() = this.age >= 18
fun <T: mut#{ age :: Int }> T.makeAdult() {
this.age = 18
}
Mike
12/06/2019, 4:59 PMLeon K
12/06/2019, 5:05 PMMike
12/06/2019, 5:09 PMLeon K
12/06/2019, 5:18 PMRuckus
12/06/2019, 6:51 PMLeon K
12/06/2019, 7:08 PMLeon K
12/06/2019, 7:09 PMMike
12/06/2019, 7:15 PMLeon K
12/06/2019, 8:18 PMraulraja
12/08/2019, 10:04 PM+
is derived automatically for Row
if there is Proof of the parts of its product type. In this case two ints. The compiler inductively derives it and establish synthetic subtype relationships replacing impossible casts with the proofs. Proofs are coherent as proposed in Keep 87 but as you can see you can abstract over the arity of product types already by simply making your behaviors target the Tuple hierarchy or any other hierarchy that is isomorphic to what you are modelling. Proofs can be added in user code as a prelude at any point and they are discovered coherently based on the extension types that defines them only allowing orphan overrides if they are internal. This is not ready yet but it will be available in Meta as the user type system DSL to draw relations between types. Some of the supported relations I have identified so far are synthetic subtyping, extensions with intersections, type refinement with predicates and negation proofs that disallow calls to types in the scopes of other types (similar to how restrict suspension works but a la carte)raulraja
12/08/2019, 10:14 PMraulraja
12/08/2019, 10:19 PMraulraja
12/08/2019, 10:24 PMQuy D X Nguyen
12/09/2019, 4:54 AMQuy D X Nguyen
12/09/2019, 4:57 AMraulraja
12/09/2019, 11:48 AMraulraja
12/09/2019, 11:49 AMraulraja
12/09/2019, 11:51 AMraulraja
12/09/2019, 11:52 AMraulraja
12/09/2019, 11:52 AMraulraja
12/09/2019, 11:52 AMraulraja
12/09/2019, 11:53 AMraulraja
12/09/2019, 11:53 AMraulraja
12/09/2019, 11:59 AMraulraja
12/09/2019, 12:02 PMraulraja
12/09/2019, 12:04 PMraulraja
12/09/2019, 12:05 PMraulraja
12/09/2019, 12:07 PMraulraja
12/09/2019, 12:07 PMraulraja
12/09/2019, 12:09 PMLeon K
12/10/2019, 8:49 AMQuy D X Nguyen
12/19/2019, 5:36 PMraulraja
12/19/2019, 6:33 PMraulraja
12/19/2019, 6:34 PMQuy D X Nguyen
12/19/2019, 11:12 PM