Skip to content

Commit

Permalink
Merge 12c1595 into abbe6b9
Browse files Browse the repository at this point in the history
  • Loading branch information
aiya000 committed Oct 13, 2018
2 parents abbe6b9 + 12c1595 commit 3fb2616
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion autoload/vital/__vital__/Data/Optional.vim
Expand Up @@ -63,7 +63,7 @@ function! s:get_unsafe(o) abort
endfunction

function! s:get_or(o, alt) abort
return get(a:o, 0, a:alt)
return get(a:o, 0, a:alt())
endfunction

function! s:has(o, type) abort
Expand Down
6 changes: 5 additions & 1 deletion doc/vital/Data/Optional.txt
Expand Up @@ -108,7 +108,11 @@ get({optional}) *Vital.Data.Optional.get()*

get_or({optional}, {alternative}) *Vital.Data.Optional.get_or()*
Returns a contained value in {optional}. If {optional} is an invalid
value, it returns {alternative} instead.
value, it returns the {alternative}'s result instead.
>
echo O.get_or(O.new(10), { -> -1 }) " 10
echo O.get_or(O.none(), { -> -1 }) " -1
<

get_unsafe({optional}) *Vital.Data.Optional.get_unsafe()*
Returns a contained value in {optional}. If {optional} is an invalid
Expand Down
16 changes: 13 additions & 3 deletions test/Data/Optional.vimspec
Expand Up @@ -96,14 +96,24 @@ Describe Data.Optional
End

Describe .get_or()
Before all
function! Negative1() abort
return -1
endfunction
End

After all
delfunction Negative1
End

It returns the content of optional value
let o = O.some(42)
Assert Equals(O.get_or(o, -1), 42)
Assert Equals(O.get_or(o, function('Negative1')), 42)
End

It returns the second argument as alternative if the first argument is invalid
It returns the second argument result as alternative if the first argument is invalid
let o = O.none()
Assert Equals(O.get_or(o, -1), -1)
Assert Equals(O.get_or(o, function('Negative1')), -1)
End
End

Expand Down

0 comments on commit 3fb2616

Please sign in to comment.