코틀린 문맥 매개변수

Kotlin context parameters

함수가 요구하는 주변 값을 타입으로 선언하고 호출 범위에서 공급합니다.

···
html
<div class="visual context" data-step="0"><div class="visual-head"><strong>CONTEXT PARAMETER</strong><i></i></div><div class="visual-scene"><div class="ctx-scope"><small>caller scope</small><strong>Logger</strong></div><div class="ctx-link"><i></i></div><div class="ctx-fn"><small>function</small><strong>writeLog()</strong><em>needs Logger</em></div></div><div class="visual-caption"></div><div class="visual-dots"><b></b><b></b><b></b></div></div>
css
.visual{width:min(95vw,760px);height:min(91vh,330px);padding:clamp(9px,2.5vmin,18px);display:flex;flex-direction:column;gap:clamp(6px,1.7vmin,11px);border:1px solid var(--line);border-radius:13px;background:var(--surface);font:500 clamp(12px,3.4vmin,16px)/1.25 var(--font-sans,-apple-system),sans-serif}
.visual-head{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:19px;color:var(--accent)}.visual-head strong{font-size:1em}.visual-head i{display:block;width:8px;height:8px;flex:none;border-radius:50%;background:var(--accent);animation:visual-pulse 1.3s infinite}@keyframes visual-pulse{50%{opacity:.25}}
.visual-scene{position:relative;flex:1;min-height:0;overflow:hidden}.visual-caption{min-height:2.4em;display:grid;place-items:center;text-align:center}.visual-dots{display:flex;justify-content:center;gap:4px}.visual-dots b{display:block;width:15px;height:3px;border-radius:3px;background:var(--line)}.visual-dots b.on{background:var(--accent)}
.visual-scene *{box-sizing:border-box}.visual-scene .mono{font-family:ui-monospace,monospace}.visual-scene .muted{color:var(--muted)}.visual-scene .lit{border-color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 14%,var(--surface))!important;color:var(--fg)!important}
.context .visual-scene{display:flex;align-items:center;justify-content:center;gap:0}.ctx-scope,.ctx-fn{height:72%;min-width:35%;border:1px solid var(--line);border-radius:9px;background:var(--bg);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:3px;text-align:center}.ctx-scope small,.ctx-fn small{font-size:12px;color:var(--muted)}.ctx-scope strong,.ctx-fn strong{font-size:clamp(12px,3.4vmin,16px)}.ctx-fn em{font:500 12px ui-monospace,monospace;color:var(--accent)}.ctx-link{height:3px;background:var(--line);flex:1;position:relative}.ctx-link i{position:absolute;top:-6px;left:0;width:15px;height:15px;border-radius:50%;background:var(--accent);transition:left .45s}.context[data-step="1"] .ctx-link i,.context[data-step="2"] .ctx-link i{left:calc(100% - 15px)}.context[data-step="0"] .ctx-scope,.context[data-step="2"] .ctx-fn{border-color:var(--accent);background:color-mix(in srgb,var(--accent) 14%,var(--surface))}
js
const root=document.querySelector('.visual'),cap=root.querySelector('.visual-caption'),dots=[...root.querySelectorAll('.visual-dots b')],captions=["Caller provides Logger","Context flows to function","Requirement is satisfied"];let step=0;function advance(){root.dataset.step=String(step);cap.textContent=captions[step];dots.forEach((d,i)=>d.classList.toggle('on',i===step));if(root.classList.contains('generic')){root.querySelector('.generic-input').textContent=step===3?'"hi"':'7';root.querySelector('.generic-output').textContent=step===3?'"hi" : string':'7 : number'}if(root.classList.contains('union'))root.querySelector('.set-value').textContent=step%2?'value: "seven"':'value: 7';step=(step+1)%captions.length}advance();let timer=setInterval(advance,1100);root.addEventListener('pointerdown',()=>{clearInterval(timer);advance()})

코틀린 문맥 매개변수는 함수가 주변에서 받아야 할 값을 시그니처에 명시합니다. 호출할 때 그 문맥을 제공하면 함수는 일반 인자처럼 매번 전달하지 않아도 값을 사용할 수 있습니다.

데모는 호출 범위의 서비스가 요구 슬롯으로 들어가는 경로를 보여줍니다. 숨은 전역 상태와 달리 요구 사항이 타입에 드러납니다. 문맥 매개변수 자체는 Kotlin 2.4에서 안정화됐지만, 같은 타입 후보가 여럿인 경우의 명시적 문맥 인자는 아직 실험 기능입니다.

언제 쓰나

로깅·트랜잭션 같은 공통 문맥을 여러 함수에 전달할 때 검토합니다. 안정 버전은 Kotlin 2.4 이상입니다.

페이지로 열기 ↗