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

KotlinでAPIを型安全にした話

noripi
December 19, 2017

 KotlinでAPIを型安全にした話

2017.12.19 R.kt#2の発表で使用したスライドです。

noripi

December 19, 2017
Tweet

More Decks by noripi

Other Decks in Technology

Transcript

  1. ͍͋ͭࣗ͝͞ݾ঺հ  ɾ໊લ/PSJZVLJ*TIJEB  ɾαʔόαΠυʢ,PUMJO+BWB1)11ZUIPO ɾΤϯδχΞ!3FUUZ ɾJ04 4XJGU0CKFDUJWF$ ɾ"84΋ͦͦ͜͜৮Δ ɾ,PUMJOྺ೥ϲ݄͘Β͍

    ɾ,PUMJO͔Θ͍͍ ɾ࠷ۙ,PUMJO͗ͯ͢͠+BWBΛ๨Ε͖ͯͨ ɾ,PUMJO/BUJWFʹ΋ظ଴͍ͯ͠Δ IUUQTRJJUBDPNOPSJQJJUFNTFFDCEBDBGCE
  2. Ҡߦͯ͠ؾ͍ͮͨ,PUMJOͷϝϦοτ   ɾσʔλϗϧμΫϥεͷఆٛ ɾฦΓ஋ͷܕΛ10+0 ී௨ͷ+BWBΦϒδΣΫτ Ͱࣄલʹఆٛ͢Δඞཁ͕͋Δ ɾ+BWBͩͱఆٛ͢Δͷ͕ͪΐͬͱΊΜͲ͍͘͞ public class

    GetRestaurantResponse { private long restaurantId; private String restaurantName; public long getRestaurantId() { return this.restaurantId; } public String getRestaurantName() { return this.restaurantName; } }
  3. Ҡߦͯ͠ؾ͍ͮͨ,PUMJOͷϝϦοτ   ɾ$MBTT%FMFHBUJPO ɾجຊతͳܕΛ૊Έ߹ΘͤͯฦΓ஋Λ࡞Γ͍ͨέʔε͸ׂͱΑ͋͘Δ ɾ+BWBͩͱఆٛ͢Δͷ͕͍ͩͿΊΜͲ͍͘͞ public class GetReviewResponse implements

    IReview, IRestaurant, IUser { private IReview review; private IRestaurant restaurant; private IUser user; public long getReviewId() { return this.review.getReviewId(); } public String getUserName() { return this.user.getUserName(); } public String getRestaurantName() { return this.restaurant.getRestaurantName(); } ... }
  4. Ҡߦͯ͠ؾ͍ͮͨ,PUMJOͷϝϦοτ   ɾ$MBTT%FMFHBUJPO ɾΠϯλϑΣʔεͷ࣮૷ΛଞͷΫϥεʹҠৡͰ͖Δ ɾQSPQFSUZΛࡉ͔͘JOUFSGBDFԽ͓͚ͯ͠͹ɺฦΓ஋ΫϥεΛࡉ͔͘ఆٛՄೳʹ data class GetReviewResponse( private

    val review: IReview, private val restaurant: IRestaurant, private val user: IUser ) : IReview by review, IRestaurant by restaurant, IUser by user { // ෳ਺ͷinterfaceͰఆ͕͔ٛͿͬͯΔͱ͖͚ͩ໌ࣔతʹࢦఆ override val userId: Int = this.user.userId }