Call function in if satement

Hey guys,

I need to call some function that return bool value, inside a if statement.
So I have this code:

            #[weight = 10_000 + T::DbWeight::get().writes(2)]
    		pub fn unlock(origin) -> dispatch::DispatchResult {
    		    let user = ensure_signed(origin)?;
                ensure!(user == Self::authority(), Error::<T>::Forbidden);
    			<Ends<T>>::put(T::BlockNumber::zero());
    			Paused::put(true);
    			Ok(())
    		}

Now I want to call it in if statement, but I don't know how, I try with this code, but returns me this:

if (Self::unlock) {
| ^^^^^^^^^^^^^^ expected bool, found fn item

If is unlock, then it should add amount on some accountId. Any help?

Self::unlock is a function, so you need to call it if you want its output like so:

if Self::unlock(origin) {...}

But given that unlock returns dispatch::DispatchResult you probably can't use that as a bool either, so you probably need to call is_ok() on it (or whatever it is you need.)

Thank for the reply, but I have another error:

if Self::unlock(origin).is_ok() {
^^^^^^ value used here after move

I think it's because I have this line:

let user = ensure_signed(origin)?;
^^^^^^ value used here after move

How can I resolve this?

Please post error messages in code blocks, not quote blocks. Also, please post full errors.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.