With

1.1.4

with(…) { … } Statement (a Swift µ-Library)
capnslipp/With

What's New

1.1.4

2022-12-29T13:46:16Z
  • Fix tests for Swift ≤5.6 type inference 1a8fa55

Minor:

  • Deleted no-longer-necessary XCTestManifests.swift & LinuxMain.swift 38a92b9

Full Changelog: 1.1.3...1.1.4

With

With is a Swift micro-library that provides a with statement— which is modeled after the with functionality found in Python, JavaScript, Visual Basic, Object Pascal, Delphi; using found in C#; and also/let found in Kotlin.

With provides a set of overloaded generic free functions that are useful for:

  • Object or value initialization and setup in a declarative style (think Swift UI's hierarchical style, but for anything, anywhere).
  • Performing multiple operations on an object/value fetched via a method/property, while staying D.R.Y and without needing to create a local var (while still avoiding invoking the method/getter repeatedly).
  • Performing calculations with an object/value that only needs to live long enough to be configured and do some calc (and you're only interested in a result object/value).

With provides a free function func with(_ subject: SubjectT, operations: (inout SubjectT) -> Void) -> SubjectT that can be used on any object or value type to do stuff like this:

// initializes a value-type `hitTestOptions` dictionary  for use with
//   `SCNView`'s `hitTest(…)` with the desired options some of which have
//   changed in newer OS versions (which the standard dictionary literal syntax
//   can't cleanly do)
let hitTestOptions = with([SCNHitTestOption : Any]()) { o in
	o[.boundingBoxOnly] = true
	o[.rootNode] = _myRootNode
	if #available(iOS 11.0, tvOS 11.0, macOS 10.13, *) {
		o[.searchMode] = SCNHitTestSearchMode.all.rawValue
	} else {
		o[.sortResults] = true
		o[.firstFoundOnly] = false
	}
}

Or like this:

// initializes the object-type `newButton` with title, sizing, styling, etc.
//   and adds the view to a superview
let newButton = with(UIButton(type: .system)) { b in
	b.titleLabel!.font = .systemFont(ofSize: 13)
	b.setTitle("My Button", for: .normal)
	b.autoresizingMask = [ .flexibleLeftMargin, .flexibleBottomMargin ]
	b.contentEdgeInsets = UIEdgeInsets(top: 6.5, left: 7, bottom: 6.5, right: 7)
	with(b.layer) { l in
		l.borderWidth = 1.0
		l.borderColor = UIColor.white.cgColor
		l.cornerRadius = 5
	}
	b.sizeToFit()
	
	rootViewController.view.addSubview(b)
}

With also has an alternate func withMap(_ subject: SubjectT, operations: (inout SubjectT) -> ReturnT) -> SubjectT form that can return an arbitrary value from the closure (instead of the value passed in):

// initializes a `DateFormatter`, configures it, and uses it to calculate a
//   `String` which is the only thing we want to hang onto
let dateString = withMap(DateFormatter()) { f in
	f.dateStyle = .medium
	f.timeStyle = .none
	f.locale = Locale(identifier: "en_US")
	
	let currentDate = Date()
	return f.string(from: currentDate)
}

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Jan 01 2023 20:34:36 GMT-0500 (GMT-05:00)