Kotlin context-sensitive resolution

코틀린 문맥 감지 이름 해석

A known expected type lets enum entries and sealed subclasses omit a qualifying type name.

···
html
<div class="r2 resolution" data-step="0"><div class="r2-head">CONTEXT RESOLUTION<span class="r2-live"></span></div><div class="r2-stage"><div class="r2-type">expected: Problem</div><div class="r2-options"><span>CONNECTION</span><span>DATABASE</span><span>OTHER</span></div><div class="r2-lookup">Problem.DATABASE</div></div><div class="r2-foot"><span class="r2-caption"></span><span class="r2-progress"></span></div></div>
css
.r2{width:min(95vw,730px);height:min(92vh,330px);padding:clamp(9px,2.5vmin,17px);display:flex;flex-direction:column;gap:7px;border:1px solid var(--line);border-radius:13px;background:var(--surface);color:var(--fg);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,sans-serif)}.r2 *{box-sizing:border-box}.r2-head{display:flex;justify-content:space-between;align-items:center;min-height:17px;color:var(--accent);font-size:12px;font-weight:700;letter-spacing:.03em}.r2-live{height:7px;width:7px;border-radius:50%;background:var(--accent);animation:r2beat 1.1s infinite}@keyframes r2beat{50%{opacity:.25}}.r2-stage{flex:1;min-height:0;position:relative;overflow:hidden}.r2-foot{min-height:27px;display:flex;align-items:center;justify-content:space-between;gap:8px;font-size:12px}.r2-caption{min-width:0}.r2-progress{flex:none;color:var(--accent);font:600 12px ui-monospace,monospace}.resolution .r2-stage{display:grid;align-content:center;justify-items:center;gap:8px}.r2-type,.r2-lookup{border:1px solid var(--line);border-radius:7px;padding:6px 10px;background:var(--bg);font:600 12px ui-monospace,monospace}.r2-options{display:flex;gap:5px;max-width:100%}.r2-options span{border:1px solid var(--line);border-radius:5px;padding:6px 5px;font:600 12px ui-monospace,monospace;transition:opacity .3s,transform .3s,background .3s}.resolution[data-step="0"] .r2-type,.resolution[data-step="1"] .r2-options span:nth-child(2),.resolution[data-step="2"] .r2-lookup{border-color:var(--accent);background:color-mix(in srgb,var(--accent) 15%,var(--surface));color:var(--accent)}.resolution[data-step="1"] .r2-options span:not(:nth-child(2)){opacity:.35}.resolution[data-step="2"] .r2-options span:nth-child(2){transform:translateY(4px)}
js
const root=document.querySelector('.r2'),caption=root.querySelector('.r2-caption'),progress=root.querySelector('.r2-progress'),labels=["Expected type is Problem","Find the matching member","Resolve its full name"];let step=0;function advance(){root.dataset.step=String(step);caption.textContent=labels[step];progress.textContent=(step+1)+' / '+labels.length;step=(step+1)%labels.length}advance();const timer=setInterval(advance,1000);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

Kotlin context-sensitive resolution uses an expected type, such as a when subject or declared variable type, to resolve a short name. A branch over Problem can write DATABASE instead of Problem.DATABASE.

The demo first establishes the expected type, then selects the matching name. This remains experimental in Kotlin 2.3 and needs a compiler option. Clashing declarations can make a short name ambiguous.

When to use

Use it to shorten enum and sealed branches, and qualify names when resolution is ambiguous.

Open as page ↗