Top > Shiba > Shiba Language > 宣言

* 宣言 [#m1a7de19]
#contents

* 変数 [#vd7ff4c2]
#code(d,){{
int x1; // デフォルト値で初期化される
int x2 = 1; // 1で初期化される
int x3 = void; // 初期化処理は特にされない
}}

* 固定長配列 [#g9508c2c]
#code(d,){{
int[3] x1; // 全てデフォルト値で初期化される
int[3] x2 = {0,1,2}; // 0,1,2で初期化される
int[3] x3 = {0,1}; // コンパイルエラー(初期化子が不足)
int[2][2] x4; // 二次元配列 全てデフォルト値で初期化される
int[2][2] x5 = { {0,1} , {2,3} };
}}

* 定数 - immutable [#n28403b5]
#code(d,){{
immutable int ONE = 1;
immutable float TWO = 2.0f;

pod Vector3
{
    float x;
    float y;
    float z;
};
immutable Vector3 BASIS_Z = {0,0,1};
}}

* 変数の属性 - const,ref,in,readonly [#t652f802]
''const''
- const属性のメンバ関数しか呼ぶことができない。(変数を変更することができない)
#code(c,){{
pod Hoge
{
  public int a()const { return 1; }
  public int b() { return 2; }

  static void test()
  {
    const Hoge hoge;
    hoge.a(); // OK
    hoge.b(); // const関数ではないので呼ぶことができない。
  }
};

}}

''ref''
- 参照型。Cでいうポインタ。
- 参照は保持することはできない。
- 関数の引数および戻り値でのみ使用可能。
- 戻り値に使う場合,関数ローカルの変数を戻り値として使うことはできない。
#code(c,){{
pod Hoge
{
  static public void a( ref int b ) 
  {
    b = 2;
  }
 
  static void test()
  {
    int hoge = 0;
    Hoge.a( hoge ); // hogeは2が入る。
  }
};

}}

''in''
- 関数の引数でのみ使用可能。
- RealTypeならconst,それ以外ならconst refのエイリアス。
- const,refと併用はできない。

''readonly''
- C#のものと同じ。
- コンストラクタでしか変更・代入ができないメンバ変数につける属性。

#code(c,){{
class Vector2
{
  float x;
  float y;
};

class Hoge
{
public:
  this()
  {
    mInt = 2; // コンストラクタからは変更可能
    @mVec2 = new Vector2();
    @mConstVec2 = new Vector2();
  }

  void example()
  {
    mInt = 3; // エラー:コンストラクタではないところから変更できない
    @mVec2 = new Vector2(); // エラー
    mVec2.x = 2; // OK
    mConstVec2.x = 2; // エラー
    mVec2.x = mConstVec2.x; // OK
  }
 
private:
  readonly int mA;
  readonly Vector2@ mVec2;
  readonly const Vector2@ mConstVec2;
};

}}
* ver2以降 [#g1939ae7]

** 固定長配列 [#g9508c2c]
#code(d,){{
int[3] x1; // 全てデフォルト値で初期化される
int[3] x2 = {0,1,2}; // 0,1,2で初期化される
int[3] x3 = {0,1}; // コンパイルエラー(初期化子が不足)
int[2][2] x4; // 二次元配列 全てデフォルト値で初期化される
int[2][2] x5 = { {0,1} , {2,3} };
}}

** 型推論 - auto [#s4ab5965]
#code(d,){{
class Hoge
{
  void func(){}
}

auto a = new Hoge(); // aはHogeと推論される
a.func();
}}

** 可変長配列 [#l6351dd8]
- 可変長配列はArrayテンプレートによって生成されるclass。

#code(d,){{
int[] x1; // Std.Array!( int ) x1; と同じ。何も指定されなければnullになる。
int[] x2 = new int[5]; // 5の長さの配列を作成
}}
** 連想配列 [#p2571b72]
- 連想配列はHashtableテンプレートによって生成されるclass。

#code(d,){{
// keyがchar型,valueがintのHashtable
int[ char ] x; // Std.Hashtable!( char , int ) x; と同じ
}}

    ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS