Created
April 3, 2009 07:02
-
-
Save ukstudio/89661 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let s:id_counter = 0 | |
let s:klass = {'_class_': ['Klass'], '_id_': 0} | |
" クラスの作成 | |
function! s:klass.create(...) dict | |
let object = deepcopy(self) | |
let class = copy(self._class_) | |
for c in get(a:1, '_class_', []) | |
call add(class, c) | |
endfor | |
let s:id_counter += 1 | |
let object._id_ = s:id_counter | |
call remove(object, 'create') | |
call extend(object, a:1) | |
let object._class_ = class | |
return object | |
endfunction | |
" インスタンスの作成 | |
function! s:klass.new(...) dict | |
let object = deepcopy(self) | |
let s:id_counter += 1 | |
let object._id_ = s:id_counter | |
if a:0 >= 1 && !empty(a:1) | |
call extend(object, a:1) | |
endif | |
return object | |
endfunction | |
" メソッド一覧 | |
function! s:klass.methods(...) dict | |
let properties = items(self) | |
let methods = [] | |
call filter(properties, 'type(v:val[1]) == 2') | |
for property in properties | |
let match = matchstr(string(property[1]), 'function') | |
if !empty(match) | |
call add(methods, property[0]) | |
endif | |
endfor | |
return methods | |
endfunction | |
" 継承関係にあるか | |
function! s:klass.is_a(class) dict | |
return index(self._class_, a:class) != -1 | |
endfunction | |
" 継承 | |
function! s:klass.extend(...) dict | |
let class = copy(self._class_) | |
call add(class, get(a:1, '_class_', [])) | |
call extend(self, a:1) | |
let self._class_ = class | |
let s:id_counter += 1 | |
let self._id_ = s:id_counter | |
return self | |
endfunction | |
function! Test() | |
" クラス定義 | |
let Person = s:klass.create({ | |
\ '_class_': ['Person'], | |
\ 'name': '', | |
\ 'age' : 0}) | |
" メソッド定義 | |
function! Person.hello() | |
echo "Hello! My name is" self.name | |
endfunction | |
let ukstudio = Person.new({'name': 'ukstudio', 'age': 22}) | |
echo ukstudio | |
call ukstudio.hello() | |
let tarou = Person.new({'name': 'tarou', 'age': 34}) | |
call tarou.hello() | |
"メソッドの一覧 | |
echo ukstudio.methods() | |
"継承する | |
let Member = Person.extend({'_class_':'Member'}) | |
let member = Member.new({'name': 'member', 'age': 12}) | |
call member.hello() | |
"継承関係にあるか | |
echo ukstudio.is_a('Klass') | |
echo ukstudio.is_a('Person') | |
echo member.is_a('Person') | |
" echo s:klass | |
" echo Person | |
" echo ukstudio | |
" echo tarou | |
" echo Member | |
" echo member | |
let Array = s:klass.new({}) | |
" オーバーライド | |
function! Array.new(...) dict | |
let i = 0 | |
for v in a:000 | |
let self[i] = v | |
let i += 1 | |
endfor | |
return self | |
endfunction | |
function! Array.values() dict | |
let result = {} | |
let values = items(self) | |
call filter(values, 'v:val[0] =~ "\\d\\+"') | |
for value in values | |
let result[value[0]] = value[1] | |
endfor | |
return result | |
endfunction | |
let list = Array.new(1, 2, 3, 4) | |
echo list.values() | |
endfunction | |
call Test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment