بتل کد Battle Codeدارد باز می‌شود…

جلسه‌ی 28: شکل‌های جنریک Generic Shapes

برچسب خالی فقط برای تابع نیست. interface Box<T> و class Stack<T> می‌سازی: یک شکل و یک کلاس که برای هر نوعی کار می‌کنند. و می‌فهمی Array<T> و Promise<T> هم از اول همین بوده‌اند.

در این جلسه یاد می‌گیری

درس

مینا برای بازی‌اش جعبه‌ی جایزه می‌خواهد. داخل یک جعبه عدد است (سکه) و داخل یکی دیگر متن (اسم یک جایزه). پس دو شکل نوشت: NumberBox و TextBox. دوباره همان درد جلسه‌ی ۲۶: دو شکلِ یک‌جور که فقط نوع value در آن‌ها فرق دارد.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface NumberBox {
  value: number;
}

interface TextBox {
  value: string;
}

const coins: NumberBox = { value: 50 };
const prize: TextBox = { value: "robot" };
show(coins.value);
show(prize.value);
</script>

همان برچسب خالی را روی شکل هم می‌شود چسباند. interface Box<T> یعنی «جعبه‌ای که رویش یک برچسب خالی است». داخل شکل، value: T یعنی «چیزی که داخل جعبه است از نوع همان برچسب است». موقع استفاده برچسب را پُر می‌کنی: Box<number> جعبه‌ی عدد است و Box<string> جعبه‌ی متن. یک شکل، برای همه.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Box<T> {
  value: T;
}

const coins: Box<number> = { value: 50 };
const prize: Box<string> = { value: "robot" };
show(coins.value + 1);
show(prize.value.toUpperCase());
</script>

برچسب فقط تزئین نیست. روی این جعبه نوشته‌ایم number، ولی داخلش متن گذاشته‌ایم. اجرا کن: خط قرمز. مثل این است که روی جعبه نوشته باشی «کتاب» و داخلش کفش بگذاری.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Box<T> {
  value: T;
}

const coins: Box<number> = { value: "fifty" };
show(coins.value);
</script>

تابع جنریک و شکل جنریک با هم خیلی خوب کار می‌کنند. این تابع هر چیزی را می‌گیرد و آن را داخل یک جعبه می‌گذارد. جوابش Box<T> است: اگر عدد بدهی، Box<number> می‌گیری. تایپ‌اسکریپت خودش می‌فهمد.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Box<T> {
  value: T;
}

function wrap<T>(value: T): Box<T> {
  return { value: value };
}

const a = wrap(50);
const b = wrap("robot");
show(a.value + 1);
show(b.value.length);
</script>

کلاس هم می‌تواند برچسب داشته باشد. یک دسته بشقاب را تصور کن: بشقاب تازه را روی دسته می‌گذاری و همیشه از رو برمی‌داری. به این می‌گویند پشته (stack). class Stack<T> پشته‌ای از هر نوعی است. push یک T می‌گیرد و pop بالاترین را برمی‌دارد. جواب pop نوع T | undefined دارد، چون شاید پشته خالی باشد. موقع ساختن می‌گویی پشته‌ی چه چیزی است: new Stack<number>().

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
class Stack<T> {
  private items: T[] = [];

  push(item: T): void {
    this.items.push(item);
  }

  pop(): T | undefined {
    return this.items.pop();
  }

  get size(): number {
    return this.items.length;
  }
}

const scores = new Stack<number>();
scores.push(10);
scores.push(20);
show(scores.pop());
show(scores.size);
</script>

و مثل همیشه، برچسب مراقب است. این پشته برای عدد ساخته شده است: new Stack<number>(). پس push("ten") خط قرمز می‌گیرد. همان کلاس، ولی هر پشته فقط نوع خودش را قبول می‌کند.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
class Stack<T> {
  private items: T[] = [];

  push(item: T): void {
    this.items.push(item);
  }
}

const scores = new Stack<number>();
scores.push(10);
scores.push("ten");
</script>

حالا یک راز: تو از جلسه‌ی ۸ با یک شکل جنریک کار می‌کنی و خبر نداشتی! number[] فقط شکلِ کوتاهِ Array<number> است. آرایه یک جعبه‌ی جنریک است که خودِ تایپ‌اسکریپت ساخته. یکی دیگر هم هست: Promise<T>، جعبه‌ای که مقدارش بعداً می‌رسد (مثل جواب یک سرور). Promise<string> یعنی «بعداً یک متن می‌رسد». در جلسه‌ی ۳۴ با آن کار می‌کنی؛ فعلاً فقط <T> را در آن بشناس.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
const scores: Array<number> = [10, 20];
const names: string[] = ["Sara", "Ali"];
scores.push(30);
show(scores.length);
show(names.join(", "));

const later: Promise<string> = Promise.resolve("done");
later.then((text) => show(text.toUpperCase()));
</script>

اشتباه رایج: برچسب را خالی می‌گذاری. تایپ‌اسکریپت برای تابع خودش برچسب را از روی ورودی پُر می‌کند. ولی وقتی نوعِ یک جعبه را می‌نویسی، باید خودت بگویی: Box<string>، نه Box خالی. این‌جا Box بدون <...> نوشته شده و خط قرمز می‌گیرد.

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
interface Box<T> {
  value: T;
}

const prize: Box = { value: "robot" };
show(prize.value);
</script>

مثال‌های این جلسه

چالش‌ها

  1. Fill In the Labels (آسان)
  2. One Result Shape (آسان)
  3. The Queue (متوسط)
  4. Pairs (متوسط)
  5. Find the Red Lines (سخت)
  6. The Pocket (سخت)

بیشتر بخوان

← جلسه‌ی 27: با شرط جلسه‌ی 29: ابزارهای آماده →