# How to use Result function?

**URL:** <https://forum.dfinity.org/t/how-to-use-result-function/5768>\
**Category:** Developers\
**Tags:** Functional-Programming\
**Created:** [July 7, 2021, 4:44pm UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768 "2021-07-07T16:44:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![harish007](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@harish007](https://forum.dfinity.org/u/harish007)\
**Post date:** [July 7, 2021, 4:44pm UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/1 "2021-07-07T16:44:23Z")

</div>

I want examples for the Result function. Can anyone help me?

---

<div class="post-metadata">

**Author:** ![3cL1p5e7](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/3cl1p5e7/32/4649_2.png) [@3cL1p5e7](https://forum.dfinity.org/u/3cL1p5e7)\
**Post date:** [July 8, 2021, 12:36pm UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/2 "2021-07-08T12:36:04Z")

</div>

Rust? What is Result function do you mean?

---

<div class="post-metadata">

**Author:** ![rbolog](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbolog/32/239_2.png) [@rbolog](https://forum.dfinity.org/u/rbolog)\
**Post date:** [July 8, 2021, 3:52pm UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/3 "2021-07-08T15:52:25Z")

</div>

Using Motoko:

1. declare Type (snippet):  
`type Result<T,E> = Result.Result<T,E>;`

2.function (snippet):

```auto
public shared query ({caller}) func exportStore() : async Result<[(Id, Instruction)],Text> {
        let isAllowed : Bool = Principal.equal(canisterOwner,caller);
        if (isAllowed) {
            #ok(Iter.toArray(instructionsRegistry.entries())); 
        } else {
            #err("Caller isn't allowed to export the store. Caller is : " # Principal.toText(caller));
        }
    };

```

---

<div class="post-metadata">

**Author:** ![cyberowl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cyberowl/32/3635_2.png) [@cyberowl](https://forum.dfinity.org/u/cyberowl)\
**Post date:** [June 5, 2022, 10:51am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/4 "2022-06-05T10:51:22Z")

</div>

Why do we need else for Result? I have noticed that it always needs to be provided even if it is followed by a switch statement.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 5, 2022, 6:02pm UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/5 "2022-06-05T18:02:49Z")

</div>

I don’t think you should. If you can share some code where you think this is necessary I might be able to help.

---

<div class="post-metadata">

**Author:** ![cyberowl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cyberowl/32/3635_2.png) [@cyberowl](https://forum.dfinity.org/u/cyberowl)\
**Post date:** [June 6, 2022, 1:06am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/6 "2022-06-06T01:06:31Z")

</div>

```
public shared ({caller}) func create_username(username: Username) : async Result.Result<Username, UsernameError> {
    let tags = [ACTOR_NAME, "create_username"];
    let principal : UserPrincipal = Principal.toText(caller);

    let valid_username : Bool = Utils.is_valid_username(username);
    let username_available : Bool = check_username_is_available(username);
    let user_has_username: Bool = check_user_has_a_username(principal);

    if (valid_username == false) {
        #err(#UsernameInvalid);
    };

    if (username_available == false) {
        #err(#UsernameTaken);
    } else {
        if (user_has_username == true) {
            #err(#UserHasUsername);
        } else {
            usernames.put(principal, username);
            username_owners.put(username, principal);

            await Logger.log_event(tags, debug_show("created"));

            #ok(username);
        };
    };
};
```

---

<div class="post-metadata">

**Author:** ![cyberowl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cyberowl/32/3635_2.png) [@cyberowl](https://forum.dfinity.org/u/cyberowl)\
**Post date:** [June 6, 2022, 1:06am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/7 "2022-06-06T01:06:36Z")

</div>

type error [M0096], expression of type  
{#err : {#UsernameInvalid}}  
cannot produce expected type  
()

---

<div class="post-metadata">

**Author:** ![cyberowl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cyberowl/32/3635_2.png) [@cyberowl](https://forum.dfinity.org/u/cyberowl)\
**Post date:** [June 6, 2022, 1:07am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/8 "2022-06-06T01:07:37Z")

</div>

I wanted to have linear execution vs nested if statements. Atleast that is the goal to make it easier to read

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [June 6, 2022, 4:11am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/9 "2022-06-06T04:11:24Z")

</div>

You might need to explicitly `return`, e.g. `return #err(#UsernameInvalid);`

---

<div class="post-metadata">

**Author:** ![cyberowl](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cyberowl/32/3635_2.png) [@cyberowl](https://forum.dfinity.org/u/cyberowl)\
**Post date:** [June 6, 2022, 4:15am UTC](https://forum.dfinity.org/t/how-to-use-result-function/5768/10 "2022-06-06T04:15:06Z")

</div>

That fixed it. Thanks 🙂
