Kotlin nested type aliases

코틀린 중첩 타입 별칭

A type alias can live inside its related class, keeping its name close to its use.

···
html
<div class="r2 alias" data-step="0"><div class="r2-head">NESTED TYPE ALIAS<span class="r2-live"></span></div><div class="r2-stage"><div class="r2-alias-scope"><div class="r2-alias-head">class RoutePlanner</div><div class="r2-alias-definition">typealias StopSet = Set&lt;Stop&gt;</div><div class="r2-alias-use">visit(stops: StopSet)</div></div><div class="r2-alias-outside">outside scope</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}.alias .r2-stage{display:flex;align-items:center;justify-content:center;gap:8px}.r2-alias-scope{display:flex;flex-direction:column;gap:5px;max-width:74%;padding:8px;border:2px solid var(--accent);border-radius:10px;background:color-mix(in srgb,var(--accent) 7%,var(--surface))}.r2-alias-head{font-size:12px;font-weight:700}.r2-alias-definition,.r2-alias-use{padding:5px;border:1px solid var(--line);border-radius:5px;background:var(--bg);font:600 12px ui-monospace,monospace;overflow-wrap:anywhere;transition:background .3s,transform .3s}.r2-alias-outside{max-width:25%;font-size:12px;color:var(--muted);opacity:.45}.alias[data-step="0"] .r2-alias-scope,.alias[data-step="1"] .r2-alias-definition,.alias[data-step="2"] .r2-alias-use{background:color-mix(in srgb,var(--accent) 16%,var(--surface));transform:translateY(-2px)}.alias[data-step="2"] .r2-alias-outside{opacity:.2}
js
const root=document.querySelector('.r2'),caption=root.querySelector('.r2-caption'),progress=root.querySelector('.r2-progress'),labels=["Class owns the alias","Define a local type name","Use it within the class"];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 2.3 stabilized type aliases nested inside another declaration. A class can keep a name for Set<Stop> close to the method that uses it instead of placing that alias at package level. An alias is another name, not a new type.

The demo declares an alias inside RoutePlanner and uses it in a method. Nested aliases cannot capture type parameters from the enclosing class.

When to use

Use it to name a long type expression within the domain that owns it.

Open as page ↗