4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity]Booの関数メモ

Last updated at Posted at 2013-08-14

ジェネリック関数

Booでジェネリック関数の宣言と、C#(など)で書かれたジェネリック関数の呼び出し

def aMethod[of T](anObject):
    CSharpClass.callMethod[of T](anObject)

C#だと[T]とするところを、[of T]のように'of'をつけるらしい。

型引数に制限をつける

C#だと、

void foo<T>()
  where T: Exception
{
  // 処理
}

のように、where句でTの型を、ある型のサブクラスに制限をすることが出来るが、
Booでは以下のようにすればコンパイルはとおるようだ。

def foo[of T(Exception)]():
    // 処理

可変長引数

def foo(format as string, *args):
    System.String.Format(format, *args)

可変長引数を受け取る場合、引数名の前に''をつける。
また、受け取った引数をC#などで書かれた可変長引数を受け取る関数に、引数の値をそのまま渡す場合にも、'
'をつける。

delegate

C#におけるdelegateは、Booではcallableと呼ぶらしい。
https://github.com/bamboo/boo/wiki/Callable-Types

callableの定義

これは、ほぼC#のdelegateと同じように使える。
以下の2つの文は同じ意味になる(と思われる)。

Boo
callable MyCallable(param1 as int, param2 as int) as bool
C#
delegate bool MyCallable(int param1, int param2);

callableの使い方

Boo
// callableの定義
callable MyCallable(aString as string)

// MyCallableを引数にとる関数caller
def caller(method as MyCallable):
    method("Hello, world")

// callerにMyCallableオブジェクトを渡す
caller( MyCallable({ s | print(s) }) )

例外送出

関数ではないが、ついでに調べたので。。。

例外を送出する場合には、raiseを使う。

raise SomeException()

Javaなどのように、new する必要はない。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?