SwifUI. Short way for using modifier methods

These helper methods allow to use modifier methods in standard for SwiftUI short way.

extension View {
	@inline(__always)
	func modify(_ block: (_ view: Self) -> some View) -> some View {
		block(self)
	}
	
	@inline(__always)
	func modify<V : View, T>(_ block: (_ view: Self, _ data: T) -> V, with data: T) -> V {
		block(self, data)
	}
}

_

DISCUSSION

Suppose you have modifier methods:

func addBorder(view: some View) -> some View {
		view.padding().border(Color.red, width: borderWidth)
}
	
	func highlight(view: some View, color: Color) -> some View {
		view.border(Color.red, width: borderWidth).overlay { color.opacity(0.3) }
}

_

Ordinar Decision

Your code may be like this:

var body: some View {
		let image = Image(systemName: "globe")
		let borderedImage = addBorder(view: image)
		let highlightedImage = highlight(view: borderedImage, color: .red)
		
		let text = Text("Some Text")
		let borderedText = addBorder(view: text)
		let highlightedText = highlight(view: borderedText, color: .yellow)
		
		VStack {
			highlightedImage
			highlightedText
		}
}

This code doesn't look like standard SwiftUI code.

_

Better Decision

Described above helper methods modify(:) and modify(:,with:) allow to write code in typical for SwiftUI short way:

var body: some View {
		VStack {
			Image(systemName: "globe")
				.modify(addBorder)
				.modify(highlight, with: .red)
			
			Text("Some Text")
				.modify(addBorder)
				.modify(highlight, with: .yellow)
		}
}
SwifUI. Short way for using modifier methods
 
 
Q