Type inference from .init()? Is it possible? Or is there a better way to do this?

I need to build a regex string within my init and initialize a property with it. I would like to use 'some RegexComponent' for the type since the compiler knows the type and the type can get rather unwieldy with many captures (especially if they are optional). This fails though because the compiler doesn't infer the type:

struct Test {
	let regex: some RegexComponent

	init(_ searchString: String) {
		regex = Regex {
			searchString
		}
	}
}

let t = Test("xxx")
let s = "xxxyyy"

s.prefixMatch(of: t.regex)

The closest thing to what you want would be

struct Test {
    private let searchString: String
    private(set) lazy var regex: some RegexComponent = Regex {
        searchString
    }

    init(_ searchString: String) {
        self.searchString = searchString
    }
}

var t = Test("xxx")
let s = "xxxyyy"

s.prefixMatch(of: t.regex)

Yeah, unfortunately, that then requires the struct to be mutable. :frowning: