[swift-evolution] Three questions about a more "dynamic" Swift for InfoQ

Robert Widmann devteam.codafi at gmail.com
Tue Sep 27 11:44:48 CDT 2016


Statically typed persistent storage solutions are far and away superior to Core Data.  Whether you’re looking for queries that are correct-by-construction with DSLs or looking for easy model construction and composability, or even just modularity and ease of use.  The ability to have the type system check your work is not antithetical to the idea of marshaling data - if anything it’s far more effective.  

- Begin with a protocol for serializable things to teach the framework about your schema:

///     decode • encode == id
public protocol Serializable {
	/// Encode a value.
	var serialize : Put<Format> { get }
	/// Decode a value.
	static var deserialize : Get<Self> { get }
}

- Provide combinators and functions attached to `Get` and `Put` to make [de]serializing aggregates easy.  

struct Person : Equatable {
	let age : UInt32
	let weight : UInt32
}

extension Person : Serializable {
	static var deserialize : Get<Person> {
		return Get.zip(
			UInt32.deserialize,
		).map(Person.init)
	}

	var serialize : Put<Person> {
		return self.age.serialize
			.then(self.weight.serialize)
	}
}

- Select a backend

public struct SqlBackend : Backend {
		func prepare(statement : String) -> Statement { }
	func insert<Entity : Serializable>(_ entity : Entity) -> Result { }
	// etc.
}

- Maybe put a little DSL on top if you’re feeling cheeky

let p : SQLQuery<[Person]> =
	select <|
	from { p in
		(where_ <|  exists <|
			from { (p : Person) -> SQLQuery<()> in
				where_(val(p.age) <= val(18))
			}).then(SQLQuery<Person>.pure(p))
}

- Serve immediately

> On Sep 27, 2016, at 12:29 PM, Ricardo Parada <rparada at mac.com> wrote:
> 
> 
>> On Sep 26, 2016, at 5:32 PM, Robert Widmann via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> No, and I think moving towards a more dynamic Swift now without as-of-yet-seen significant justification would be a mistake.  Swift should be as static as possible.  We should be making every attempt to pare down the existing runtime and quietly transition those that rely on dynamism to checked reflection (yes, that is not in fact an oxymoron).  The more the compiler knows about your program, the better it does.  Period.
>> 
> How would you implement something like Core Data in pure Swift on the server where the Obj-C runtime is not available?
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160927/af9af120/attachment.html>


More information about the swift-evolution mailing list