型なし言語のための型

 型なし言語のための型

富山Ruby会議01

1fab9d01b25e99522f3dfd01e3d4cb51?s=128

Soutaro Matsumoto

November 03, 2019
Tweet

Transcript

  1. 8.

    ૬ؔਤ type-profiler Steep Sorbet RDL RBS (stdlib types) Level 1

    ܕݕࠪ Level 2 ܕݕࠪ Type signature language
  2. 9.

    ૬ؔਤ type-profiler Steep Sorbet RDL RBS (stdlib types) Level 1

    ܕݕࠪ Level 2 ܕݕࠪ Type signature language ࢖༻ ࢖༻
  3. 10.

    ૬ؔਤ type-profiler Steep Sorbet RDL RBS (stdlib types) Level 1

    ܕݕࠪ Level 2 ܕݕࠪ Type signature language ࢖༻ ࢖༻ Ruby3ʹಉࠝ ޷͖ͳ΋ͷΛΠϯετʔϧ
  4. 15.

    class Person def name @name end def name=(value) @name =

    value end end if rand(10) < 5 person = Person.new person.nme = "Soutaro Matsumoto" # NoMethodError end w ܕݕࠪʹΑͬͯɺ໢ཏతʹʢJGͷத΋ʣݕࠪ͞Εͯɺ࣮ ߦલʹ໰୊͕ݕग़͞ΕΔʢίϯύΠϧ࣌ʣ w ܕݕࠪ͠ͳ͍৔߹ʹ͸ɺϓϩάϥϜͷ࣮ߦঢ়گʹΑͬ ͯ͸ɺ໰୊͕ݟ͔ͭΒͳ͍ʢ࣮ߦ࣌ʣ
  5. 18.

    4DIFNF 4.- )BTLFMM +BWB 3VCZ +BWB4DSJQU 1)1 1ZUIPO 5ZQF4DSJQU 4XJGU

    ܕਪ࿦ 4PGU5ZQJOH (FOFSJDT '+ (SBEVBM5ZQJOH
  6. 19.

    4DIFNF 4.- )BTLFMM +BWB 3VCZ +BWB4DSJQU 1)1 1ZUIPO 5ZQF4DSJQU 4XJGU

    ܕਪ࿦ 4PGU5ZQJOH (FOFSJDT '+ (SBEVBM5ZQJOH
  7. 20.

    4DIFNF 4.- )BTLFMM +BWB 3VCZ +BWB4DSJQU 1)1 1ZUIPO 5ZQF4DSJQU 4XJGU

    ܕਪ࿦ 4PGU5ZQJOH (FOFSJDT '+ (SBEVBM5ZQJOH
  8. 21.

    .-ͷܕਪ࿦ w ʮ࠙੾ஸೡʹશ෦ͷࣜʹܕΛॻ͔ͳͯ͘΋ྑ͍ʯͱ͍͏ൃݟ w ύϥϝʔλଟ૬ʢେମ(FOFSJDTͷ͜ͱʣ # let rec map f

    xs = match xs with [] -> [] | x::xs -> f x :: map f xs;; val map : ('a -> 'b) -> 'a list -> 'b list = <fun>
  9. 24.

    6OJPOUZQFT def f(x) if x 1 else "2" end end

    (bool) -> (Integer | String)
  10. 25.

    'MPXTFOTJUJWF5ZQJOH def g(n) n.abs # NoMethodError n.bytes # n: Integer

    | String case n when Integer n.abs # n: Integer when String n.bytes # n: String end end (Integer | String) -> Integer
  11. 28.

    3VCZͰࢼͨ͠ͱ͜Ζ w ΍ͬͺΓݫ͍͠ w .-ͷܕਪ࿦Ͱ͸ॲཧͰ͖ͳ͍ఆ͕ٛͰͯ͘Δ "SSBZNBQ  w Ϋϥεఆ໋͕ٛྩతʹมΘ͍ͬͯ͘ͷ͕ݫ͍͠ʢϝλϓϩάϥϛϯάʣ w

    ͦ΋ͦ΋յΕ͍ͯΔఆ͕ٛͨ͘͞Μ͋Δ w "SSBZTFMFDUWT0CKFDUTFMFDU w )BTIΛฦ͢IBTIΛఆٛͨ͜͠ͱ͕ͳ͍ਓ
  12. 32.

    ܕͳ͠ݴޠͷͨΊͷܕ w ͦΕͳΓʹڧྗͳܕγεςϜ w 6OJPOUZQFT qPXTFOTJUJWFUZQJOH HFOFSJDT  w μοΫλΠϐϯά

    w ߏ଄త෦෼ܕ w Ϋϥεͷܧঝؔ܎Λ͋ͱͰมߋͰ͖Δ࢓૊Έ )BTLFMM 4XJGU $  w (SBEVBM5ZQJOH
  13. 35.

    5ZQF4DSJQU w ೥ݱࡏͰҰ൪উ͍ͬͯΔܕ͖ͭํݴ w λʔήοτݴޠ͸+4ʢϥϯλΠϜ΋+4ʣ class Drawer<ClothingType> { contents: ClothingType[]

    = []; add(object: ClothingType) { this.contents.push(object); } } interface Sock { color: string } interface TShirt { size: "s" | "m" | "l" } const drawer: Drawer<Sock> = new Drawer() drawer.push({ color: "white" }) drawer.push({ size: "s" })
  14. 41.

    ͨ·ͨ·ಉ͡ߏ଄͕͋ͬͨΒʁ w 5γϟπͱζϘϯ͕۠ผͰ͖ͳ͘ͳΔ w #SBOEFEUZQFͰରԠ͢Δ interface TShirt { size: "s"

    | "m" | "l"; } interface Pants { size: "s" | "m" | "l"; } interface TShirt { type: "tshirt" size: "s" | "m" | "l"; } interface Pants { type: "pants" size: "s" | "m" | "l"; }
  15. 42.

    6OJPO5ZQFT'MPXTFOTJUJWF5ZQJOH w OVMMVOEFpOFEͱ͏·͘΍ΕΔΑ͏ʹͳͬͨ const x: string | undefined = ...

    // Object is possibly 'undefined'. x.toUpperCase() if (x) { x.toUpperCase() }
  16. 49.

    ૊ΈࠐΈͷܕ஫ऍߏจʢෆ࠾༻ʣ class Box extend T::Sig extend T::Generic Elem = type_member

    sig {returns(Elem)} # @type var box: Box[Integer] box = Box.new box.x = "hello" &NCFEEFE%4- 4PSCFU $PNNFOUT 4UFFQ class Box [A] def value: A; ...; end def value=(value: A): A; ...; end end a: Box[Integer] = Box.new
  17. 50.

    ͦΕͳΓʹϦονͳܕ w λϓϧɺϨίʔυ
 [Integer, Symbol] w Ϧςϥϧܕ
 1, "hello", :world

    w 6OJPOUZQFT
 Integer | String w 0QUJPOBMUZQFTɺ(FOFSJDTʜ
 Array[String]? w յΕ͍ͯΔ΋ͷ͸JODPNQBUJCMF
 incompatible
 def select: [A] () { (Elem) -> A
  18. 51.
  19. 52.

    ࣮ߦ࣌ݕ͚ࠪͩͰ΋ w γάωνϟΛॻ͍ͨΒɺϢχοτςετͰςετ͢Δํ๏Λఏڙ w ϝιουݺͼग़͠ͷલޙʹܕݕࠪΛૠೖ w IUUQTSIDDPOOQBTTDPNFWFOU $ RBS_TEST_TARGET='Goodcheck::*' \

    RBS_TEST_RAISE=true \ RUBYOPT='-rbundler/setup -rruby/signature/test/setup' \ RBS_TEST_OPT='-rset -rpathname -Isig' \
 bundle exec rake test
  20. 54.

    ·ͱΊ w 4PGUUZQJOHΈ͍ͨʹਪ࿦ΛؤுΔͷ͸߹ཧతͰ͸ͳ͍ w ܕΛॻ͔ͳ͍ͷ͸ݫ͍͠ w ਓؒ͸ܕΛॻ͘ʢ͜͜೥ͰಘΒΕͨ஌ݟʣ w 3VCZͷͨΊͷܕ w

    ΘΓͱϦονͳܕ(FOFSJDTɺ6OJPOUZQFTɺ'MPXTFOTJUJWFUZQJOH w μοΫλΠϐϯά͸ؤுΔʢߏ଄త෦෼ܕʣ
  21. 55.

    w <$BSUXSJHIU >3$BSUXSJHIUBOE.'BHBO4PGUUZQJOH w <.BUTVNPUP >4.BUTVNPUPBOE:.JOBNJEF5ZQF*OGFSFODFGPS3VCZ 1SPHSBNTCBTFEPO1PMZNPSQIJD3FDPSE5ZQFT w <+FOTFO >4)+FOTFOBOE".”MMFSBOE15IJFNBOO

    5ZQF"OBMZTJTGPS +BWB4DSJQU w <4JFL >+4JFLBOE85BIB (SBEVBM5ZQJOHGPS0CKFDUT w <)PDITUBEU >45)PDITUBEU 5ZQFE4DIFNF'SPN4DSJQUTUP1SPHSBNT w