Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Understanding Subtype in 5 Minutes

Understanding Subtype in 5 Minutes

B936321bf044487eaf990b605a8826d3?s=128

Ryosuke TAKASHIMA

September 06, 2019
Tweet

Transcript

  1. ̑෼ͰΘ͔Δ෦෼ܕ ΤϜεϦʔ ߴౡ྄༞ʢ@rst76ʣ

  2. ͳͥʁ

  3. ෦෼ܕͱ͸ w ܕͷ্Լʢแؚʣؔ܎෦෼ܕؔ܎ w ԼҐͷܕΛ෦෼ܕͱݺͿ w +BWBͷ৔߹ɺܧঝͰ࣮ݱ w ܕͷมԽʹͱ΋ͳ͏෦෼ܕؔ܎ͷมԽ
 ෆมɾڞมɾ൓ม

    Object Number Integer ্Ґͷܕ ਌ ԼҐͷܕ ࢠ
  4. ෦෼ܕؔ܎̍ w ্Ґͷܕ͕ظ଴͞Ε͍ͯΔͱ͜ΖʹɺԼҐͷܕΛ࢖͏͜ͱ͸Ͱ͖Δ Function<Object, String> objFunc = o -> o.toString()

    + "!!!"; objFunc.apply(76); // => "76!!!" objFunc.apply("ten"); // => "ten!!!"
  5. ෦෼ܕؔ܎̎ w ԼҐͷܕ͕ظ଴͞Ε͍ͯΔͱ͜Ζʹɺ্ҐͷܕΛ࢖͏͜ͱ͸Ͱ͖ͳ͍ Function<Integer, String> intFunc = i -> Integer.toString(i

    / 2) + "???"; intFunc.apply(76); // => "39???" intFunc.apply(new Object()); // => Compile Error
  6. ؔ਺ͷ৔߹̍ w ؔ਺΋ܕ Function<Function<Number, String>, String> numFuncFunc = f ->

    f.apply(76.0); Function<Number, String> numFunc = n -> Integer.toString(n.intValue() / 4) + "..."; numFuncFunc.apply(numFunc); // => "19..."
  7. ؔ਺ͷ৔߹̎ w Ҿ਺Ͱ౉ؔ͢਺ͷҾ਺͸ɺԼҐͷܕʹͰ͖ͳ͍ʂ
 ౉ؔ͢਺͸ɺҾ਺ͷܕʹ࿙Εͳ͘ରԠͰ͖Δʢ૝ఆ֎Ͱͳ͍ʣඞཁ͕͋Δ Function<Function<Number, String>, String> numFuncFunc = f

    -> f.apply(76.0); Function<Integer, String> intFunc = i -> Integer.toString(i * 2) + "???"; numFuncFunc.apply(intFunc); // => CompileError
  8. ؔ਺ͷ৔߹̏ w ٯʹ্ҐͷܕΛҾ਺ʹͱΔؔ਺ͳΒɾɾɾ Function<Function<? super Number, String>, String> numFuncFunc =

    f -> f.apply(76.0); Function<Object, String> objFunc = o -> o.toString() + "!!!"; numFuncFunc.apply(objFunc); // => "76.0!!!"
  9. ·ͱΊ w ܕͷ্Լؔ܎͸ɺؔ਺ͷҾ਺Ͱٯస͢Δˠ൓ม w ܕ͸ָ͍͠ʂ w ؔ਺͸ָ͍͠ʂʂ

  10. એ఻ w ʰϫϯεɾΞϙϯɾΞϯɾΞϧΰϦζϜʱ
 ɹͱ͍͏ຊΛ຋༁͠·ͨ͠ w ༗໊ͳөըΛ୊ࡐʹɺܭࢉػՊֶΛղઆ͍ͯ͠·͢