Swift isolated conformance

스위프트 격리된 프로토콜 준수

Bind a protocol conformance to an actor so only safe call contexts can use it.

···
html
<div class="v"><header><b>ACTOR CONFORMANCE</b><span id="state"></span></header><main><div class="conf"><div class="call" id="call">protocol call</div><div class="gate" id="gate">GATE</div><div class="target">@MainActor<br>state</div></div><div class="verdict" id="verdict"></div></main><footer>compiler checks the conformance context</footer></div>
css
.v{width:min(92vw,660px);height:min(86vh,310px);box-sizing:border-box;padding:clamp(9px,2.5vmin,18px);border:1px solid var(--line);border-radius:14px;background:var(--surface);display:flex;flex-direction:column;gap:7px;color:var(--fg);font:500 clamp(15px,3.2vmin,17px)/1.25 var(--font-sans, sans-serif)}.v header,.v footer{display:flex;justify-content:space-between;align-items:center;gap:8px;white-space:nowrap}.v header b{color:var(--accent);font-size:.9em}.v header span,.v footer{color:var(--muted);font-size:.82em}.v main{flex:1;min-height:0;position:relative;overflow:hidden}.v .mono{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.v .active{background:var(--accent)!important;color:var(--bg)!important;border-color:var(--accent)!important}.v .muted{opacity:.28} .conf{height:70%;display:flex;align-items:center;justify-content:space-around;gap:4px}.call,.target{width:38%;box-sizing:border-box;border:1px solid var(--line);border-radius:7px;padding:12px 3px;text-align:center;font:600 clamp(12px,2.7vmin,16px) var(--font-sans, sans-serif)}.gate{width:17%;border:2px solid var(--accent);border-radius:5px;padding:8px 0;text-align:center;color:var(--accent);font:700 clamp(12px,2.7vmin,16px) var(--font-sans, sans-serif)}.verdict{text-align:center;color:var(--accent);font:600 clamp(12px,2.7vmin,16px) var(--font-sans, sans-serif)}
js
let n=0;function draw(){document.getElementById("call").textContent=n?"off actor call":"MainActor call";document.getElementById("gate").textContent=n?"NO":"YES";document.getElementById("gate").style.borderColor=n?"var(--accent-2)":"var(--accent)";document.getElementById("verdict").textContent=n?"conformance unavailable":"conformance available";document.getElementById("state").textContent=n?"unsafe context":"safe context";n=1-n}draw();setInterval(draw,1400)

Swift 6.2 can mark a protocol conformance as actor-isolated when its implementation reads MainActor state. The compiler permits use of that conformance only in the matching actor context.

A context that could invoke the protocol requirement off actor cannot use the isolated conformance. The demo gate represents availability of the conformance, not movement of the object itself.

When to use

Use it when a type implements a synchronous protocol requirement using UI state.

Open as page ↗