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

polyglot になろう !!

E86b46be0f4e61db6b28becc5493bbab?s=47 januswel
August 05, 2017

polyglot になろう !!

E86b46be0f4e61db6b28becc5493bbab?s=128

januswel

August 05, 2017
Tweet

Transcript

  1. polyglot ʹͳΖ͏ !! builderscon 2017 8/5 by januswel 1

  2. ͠Ό΂Δͻͱ januswel ΍͵͢ ޷͖ͳݴޠ C++ JavaScript React Native Japan react-native-router-flux

    react-native-sound 2 janus_wel januswel
  3. ͠Ό΂Δ͜ͱ polyglot ͱ͸ ? polyglot ͷϝϦοτ polyglot ʹͳΔʹ͸ 3

  4. polyglot ͱ͸ ? 4

  5. polyglot ͱ͸ ? 5

  6. polyglot ͱ͸ ? 6 > ໊ࢺ > ਺͔ࠃޠʹ௨͡Δਓ; ਺͔ࠃޠͰॻ͍ͨ ຊ[੟ॻ];

    ଟݴޠͷࠞަ
  7. polyglot ͱ͸ ? 7 సͯ͡ ϓϩάϥϜݴޠΛෳ਺ѻ͑Δͻͱ ͱ͍͏ҙຯͰ࢖ΘΕΔ

  8. polyglot ͷϝϦοτ 8

  9. ͔͍͍ͬ͜ 9

  10. ͋Δݴޠͷ֓೦Λผݴޠ ʹ࣋ͪࠐΉ͜ͱ͕Ͱ͖Δ 10

  11. ྫ͑͹ C++ ʹ͓͚Δ NVI ΠσΟΦϜ C++ ʹ͓͚Δ RAII ΠσΟΦϜ Eiffel

    ͷܖ໿ʹΑΔઃܭ Objective-C ͷ໋໊نଇ 11
  12. NVI ΠσΟΦϜ 12 Non Virtual Interface public ͳϝϯόʔؔ਺ΛԾ૝ؔ਺ͱ͠ͳ͍ઃܭ from C++

  13. NVI ΠσΟΦϜ 13 1 #include <iostream> 2 class Animal {

    3 public: 4 virtual ~Animal() { } 5 virtual void greet() const = 0; 6 }; 7 class Dog : public Animal { 8 public: 9 virtual void greet() const { 10 std::cout << "bow wow"; 11 } 12 }; 13 class Cat : public Animal { 14 public: 15 virtual void greet() const { 16 std::cout << "meow"; 17 } 18 }; ΦϒδΣΫτࢦ޲ͷ Α͋͘Δྫ from C++
  14. NVI ΠσΟΦϜ 14 1 #include <iostream> 2 class Animal {

    3 public: 4 virtual ~Animal() { } 5 void greet() const { 6 std::cout << call(); 7 } 8 9 protected: 10 virtual char const * call() const = 0; 11 }; 12 class Dog : public Animal { 13 protected: 14 virtual char const * call() const { 15 return "bow wow"; 16 } 17 }; 18 class Cat : public Animal { 19 protected: 20 virtual char const * call() const { 21 return "meow"; 22 } 23 }; greet() Λඇ Ծ૝ؔ਺ʹ Ծ૝ؔ਺ call() Λ௥Ճ from C++
  15. NVI ΠσΟΦϜ 15 2 class Animal { 3 public: 4

    virtual ~Animal() { } 5 void greet() const { 6 std::cout 7 << call() << "\n" 8 << call() 9 << std::endl; 10 } 11 12 protected: 13 virtual char const * call() const = 0; 14 }; ڞ௨͢ΔৼΔ෣͍Λมߋ͠΍͍͢ 2 ճ໐͘Α͏ ʹมߋ վߦΛ௥Ճ ࢠΫϥεʹ खΛՃ͑ͣ มߋՄೳ !! from C++
  16. RAII ΠσΟΦϜ 16 from C++ Resource Acquisition Is Initialization Ϧιʔεͷ֬อͱ։์Λ

    ΦϒδΣΫτϥΠϑαΠΫϧͰ؅ཧ͢Δ
  17. RAII ΠσΟΦϜ 17 from C++ 1 int main(const int argc,

    const char* const argv[]) { 2 int * const p = new int; 3 // some procedures 4 delete p; 5 6 return 0; 7 } Α͋͘Δ ? ྫ ྫ֎͕ඈͿͱ
  18. 1 template <typename T> 2 class Pointer { 3 public:

    4 Pointer(T * const pointer) : _pointer(pointer) { } 5 ~Pointer() { 6 delete _pointer; 7 } 8 private: 9 T * const _pointer; 10 }; 11 12 int main(const int argc, const char* const argv[]) { 13 Pointer<int> p(new int); 14 // some procedures 15 16 return 0; 17 } RAII ΠσΟΦϜ 18 from C++ σετϥΫλʔ Ͱղ์ Ϧιʔε֬อͱ ॳظԽΛಉ࣌ʹ ྫ֎͕ඈΜͰ ΋ղ์͞ΕΔ
  19. ܖ໿ʹΑΔઃܭ 19 Design by Contract ͋Δίʔυ܈Λ࢖͏ଆ Client ͱ࢖ΘΕΔଆ Supplier Ͱຬͨ͢΂͖ࣄલ৚݅

    Obligations ɺ ࣄޙ৚݅ Benefits ɺෆม৚݅ Invariants Λ໌֬ ʹఆٛ͠ɺಈ࡞ͨ͠ͱ͖ʹνΣοΫͰ͖ΔΑ ͏ʹ͢Δ https://www.eiffel.com/values/design-by-contract/introduction/ from Eiffel
  20. 1 class DICTIONARY [ELEMENT] 2 feature 3 put (x: ELEMENT;

    key: STRING) is 4 -- Insert x so that it will be retrievable through key. 5 require 6 count <= capacity 7 not key.empty 8 ensure 9 has (x) 10 item (key) = x 11 count = old count + 1 12 end 13 -- ... Interface specifications of other features ... 14 invariant 15 0 <= count 16 count <= capacity 17 end ܖ໿ʹΑΔઃܭ 20 ࣄલ৚݅ ࣄޙ৚݅ ෆม৚݅ from Eiffel
  21. ܖ໿ʹΑΔઃܭ 21 in JavaScript 1 export function zeroPadding(n: number, digit:

    number = 8): string { 2 assert(0 <= n) 3 assert(1 < digit) 4 const zeros = Array(digit - 1).fill('0').join('') 5 return `${zeros}${n.toString()}`.slice(-digit) 6 } 7 8 zeroPadding(32, 3); 9 zeroPadding(-4, 3); ࣄલ৚݅ ࣄલ৚݅ҧ൓ ࣄޙ৚݅΍ෆม৚݅͸ݺͼग़͠ଆͷڠྗ͕ඞཁ͕ͩ assert ͕͋Ε͹ܖ໿ʹΑΔઃܭΛ࣮ݱͰ͖Δ from Eiffel
  22. JavaScript ͷ assert ͱܕ http://qiita.com/janus_wel/items/da1fcfc8d50969135b31 assert: unassert https://github.com/unassert-js/unassert ܕ :

    flow https://flow.org/ 22
  23. Objective-C ໋໊نଇ ໌֬ͳ୯ޠΛ࢖͏ do ΍ does ͳͲ࢖Θͳ͍ ୯ޠΛུ͞ͳ͍ ઌ಄Λಈࢺͱ͢Δ etc

    23 https://developer.apple.com/jp/documentation/CodingGuidelines.pdf
  24. Objective-C ໋໊نଇ will willChangeStatusBarFrame did windowDidMove 24 https://developer.apple.com/jp/documentation/CocoaEncyclopedia.pdf

  25. Objective-C ໋໊نଇ componentDidMount componentWillReceiveProps componentWillUpdate componentWillUnmount 25 in React.js

  26. ಛఆͷͻͱͭͷݴޠʹ͍ͭͯ ΋ΑΓਂ͍ཧղΛಘΒΕΔ 26

  27. ݴޠ΁ͷཧղ͕ਂ·Δ ݴޠΛผࢹ఺ͰோΊΔ͜ͱ͕Ͱ͖ΔͷͰɺ ΑΓཧղ͕ਐΉ C ݴޠͷϙΠϯλʔͱ Java ͷࢀর౉͠ C++ ͷ move

    semantics ͱ Rust ͷॴ༗ݖ ԋࢉࢠͷ༏ઌॱҐͱ݁߹ੑ 27
  28. ϙΠϯλʔ 28 ϝϞϦʔϨΠΞ΢τ্ͷ͋Δ Ұ఺Λࢦࣔ͢͠ͷ͕ϙΠϯλʔ ϙΠϯλʔΛ௨ͯ͡த਎Λࢀ রͰ͖Δ 0x00 0x01 0x02 0xff

    b u i … \0
  29. ࢀর౉͠ 29 b u i … \0 ࢀর౉͠͸ϙΠϯλʔͷଘࡏ Λҙࣝ͠ͳ͍Α͏ʹ͚ͨͩ͠ 0x00

    0x01 0x02 0xff
  30. 1 #include <string> 2 #include <iostream> 3 4 int main(const

    int argc, const char* const argv[]) { 5 std::string a = "foo"; 6 std::string b = std::move(a); 7 8 std::cout << a << std::endl; 9 10 return 0; 11 } move semantics 30 a ͔Β b ΁ Ϝʔϒ ίϯύΠϧ͸௨Δ͕ Կ΋දࣔ͞Εͳ͍
  31. 1 let a = "foo"; 2 let b = a;

    3 4 println!(a); ॴ༗ݖ 31 a ͔Β b ΁ॴ༗ݖ ΛҠ͢ ಉ֓͡೦ΛΑΓγϯϓϧʹදݱ͍ͯ͠Δ ίϯύΠϧ Τϥʔ
  32. 2 ͭΊҎ߱ͷݴޠ͸༰қ ʹशಘͰ͖Δ 32

  33. ݴޠशಘ 33 > ݴޠͱ͍͏΋ͷ͸ɺΑͬΆͲͷʮͰ͖ͦ͜ͳ ͍ʯͰͳ͍ݶΓɺ͢Ͱʹ஌͍ͬͯΔݴޠ͔Β؆୯ ʹֶ΂ΔͷͰ͢ Thomas E. Kurtz ݴޠઃܭऀ͕ͨͪߟ͑Δ͜ͱ

  34. ݴޠͷߏ੒ཁૉ͸ ࣅ௨͍ͬͯΔ ࣜɾจ ԋࢉࢠ ੍ޚߏจ ؔ਺ / ϝιου ίϝϯτ etc

    34
  35. ৽͍͠ݴޠ͸طଘݴޠ͔Β ӨڹΛड͚͍ͯΔ 35 https://github.com/taisa831/Langury

  36. 36 Perl ͷܥේ

  37. ࡞Δ͜ͱͷͰ͖Δ΋ͷͷ ෯͕޿͕Δ 37

  38. ֤ݴޠ͝ͱʹ ಘҙͳྖҬ͕͋Δ Python: ਺ֶɺػցֶश Ruby: Web αʔϏε JavaScript: ϑϩϯτΤϯυ C++:

    ήʔϜ etc 38
  39. polyglot ʹͳΔʹ͸ 39

  40. ίετ ? ΍Δؾ ࣌ؒ 40 ࡞Γ͍ͨ΋ͷ͕͋Ε͹໰୊ʹͳΒͳ͍

  41. εςοϓͱͯ͠ᶃ ·ͬͨ͘ผͷྖҬʹखΛग़ͯ͠ΈΔ Web αʔϏεΛ࡞͍ͬͯͨͳΒήʔϜ΍Ϟ όΠϧΞϓϦɺػցֶशɺ IoT ͳͲ 41

  42. εςοϓͱͯ͠ᶄ ଎౓ؚΊͨ UX ʹͩ͜Θͬͨ Web αʔϏεΛ૊ΜͰΈΔ όοΫΤϯυ Ruby / Golang

    etc ϑϩϯτΤϯυ JavaScript React Native ͰωΠςΟϒػೳΛ࢖ͬͨϞόΠϧΞϓϦ Λ࡞ͬͯΈΔ JavaScript + Objective-C / Java (+ Swift / Kotlin) 42
  43. ·ͱΊ ࡞ΕΔ΋ͷͷ෯͕޿͕Δ ݱࡏ࢖͍ͬͯΔݴޠͷཧղ͕ਂ·Δ ͲΜͲΜָ͘͠ͳΔ 43 polyglot ʹͳΔ͜ͱͰ ͥͻ΍ͬͯΈΑ͏