edo1z blog

プログラミングなどに関するブログです

Dart言語を勉強してみる

dart.dev

Dartをインストール

Get the Dart SDK | Dart

$ dart --version
Dart VM version: 2.4.1 (Unknown timestamp) on "linux_x64"

コンソールで実行

dart hoge.dart ってやると実行された。

void main() {
  var name = 'taro';
  print('Hello ' + name);
}
$ dart hoge.dart
Hello taro

文法の概要

Language samples | Dart

ぱっと見はjavascriptに似てる。

void main() {
  var name = 'taro';
  print('Hello ' + name);

  var foods = ['apple', 'orange', 'ramen'];
  for (String food in foods) {
    print(food.toUpperCase());
  }
  int num = addTen(10);
  print(num);

  List<int> numList = [1, 2, 3]; 
  numList.forEach((num) => print(addTen(num)));

  Food food = Food('apple', 400);
  food.selfIntroduction();
}

int addTen(int n) {
  return n + 10; 
}

class Food {
  String name;
  int price;
  String description;

  Food(this.name, this.price) {

  }

  void selfIntroduction() {
    print('this food is $name');
    print('price is $price yen');
    if (description != null) print(description);
  }
}
$ dart hoge.dart
Hello taro
APPLE
ORANGE
RAMEN
20
11
12
13
this food is apple
price is 400 yen

型がしっかりしてるjavascriptみたいな感じで、async / awaitも使えるらしい。classとか型とかもうちょっと調べよう。

エディタは何を使ったらいいでしょうか?

webstormがよろしいかと思います。それか、vimでしょうか?私はwebstormにします。といってもvimでも色つけたりはしたいです。

vimプラグイン

公式プラグインだそうです。

github.com

vimプラグインの入れ方は毎回綺麗に忘れますのでメモります。私はvim-plugというのを使っているようです。

.vimrc

call plug#begin('~/.vim/plugged')

...

Plug 'dart-lang/dart-vim-plugin'

...

call plug#end()

上記のような感じで Plug 'dart-lang/dart-vim-plugin' を追加します。 そして、vimでファイルを開いて、:PlugInstall を実行します。 するとこんな感じでコードに色がつきまっす。

f:id:edo1z:20190827183401p:plain

お試しコード

void main() {
  var name = 'taro';
  print('Hello ' + name);

  var foods = ['apple', 'orange', 'ramen'];
  for (String food in foods) {
    print(food.toUpperCase());
  }
  int num = addTen(10);
  print(num);

  List<int> numList = [1, 2, 3];
  numList.forEach((num) => print(addTen(num)));

  Food food = Food('apple', 400);
  food.selfIntroduction();

  print(food.name);
  print(food.price);

  Food2 food2 = Food2('apple2', 800);
  print(food2.name);
  print(food2.price);
  food2.selfIntroduction();

  food.hoge();
  food2.hoge();

  Ojisan mr_o = Ojisan();
  mr_o.pawahara();
  mr_o.kareisyu();
  mr_o.kosyu();
}

int addTen(int n) {
  return n + 10;
}

class Food with HogeMix {
  String name;
  int price;
  String description;

  Food(this.name, this.price) {
    this.price = addTen(this.price);
  }

  void selfIntroduction() {
    print('this food is $name');
    print('price is $price yen');
    if (description != null) print(description);
  }
}

class Food2 extends Food {
  Food2(name, price)
      : super(name, price);

  @override
  void selfIntroduction() {
    print('こんにちは');
  }
}

class HogeMix {
  int num = 100;

  void hoge() {
    print(num);
  }
}

abstract class abstractOjisan {
  void kareisyu();

  void pawahara() {
    print('パワハラ');
  }
}

class interfaceOjisan {
  void kosyu() {
    print('お口臭い');
  }
}

class Ojisan extends abstractOjisan implements interfaceOjisan {
  void kareisyu() {
    print('くさくさ');
  }

  void kosyu() {
    print('くちくっさwww');
  }

  void ikemen() {
    print('僕イケメン');
  }
}
$ dart hoge.dart
Hello taro
APPLE
ORANGE
RAMEN
20
11
12
13
this food is apple
price is 410 yen
apple
410
apple2
810
こんにちは
100
100
パワハラ
くさくさ
くちくっさwww