# How to define a function that returns an object of unknown shape?

**URL:** <https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515>\
**Category:** Language Support\
**Created:** [December 17, 2022, 3:16pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515 "2022-12-17T15:16:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![blabagastered](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/blabagastered/32/8592_2.png) [@blabagastered](https://forum.dfinity.org/u/blabagastered)\
**Post date:** [December 17, 2022, 3:16pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/1 "2022-12-17T15:16:29Z")

</div>

Is it possible to define a function that returns an object of unknown shape, so that it would allow returning something like

```auto
 let basic = {
      example = 0.0;
      another = 2.2;
      third = {
        a = {
          b = 2;
          c = 3;};
        b = 2;}
    };

```

but also something like

```auto
let just_one = {
     onething = "true";
};

```

What’s the syntax for that?:  
`public func returnObjectOfUnknownShape(arg1: Text) : [what goes here?] {`

Or should I be using some other type for this kind of structured data? Which one?

---

<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:** [December 17, 2022, 4:51pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/2 "2022-12-17T16:51:27Z")

</div>

You can use a variant:

```auto
type MyVariant = {
  #basic : {
    example : Float;
    another : Float;
    third : {
      a : {
        b : Nat;
        c : Nat;
      };
      b : Nat;
    };
  };
  #onething : Text;
};

```

```auto
func returnObjectOfUnknownShape(arg1: Text) : MyVariant {
  if (arg1 == "") {
    #basic {
      example = 0.0;
      another = 2.2;
      third = {
        a = {
          b = 2;
          c = 3;
        };
        b = 2;
      }
    }
  } else {
    #onething "true"
  }
};

```

My talk from Motoko Bootcamp might help gain an intuition for them:

[![](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/9/954fb4600169a53c5ffc576046cfccee6ce66a39.jpeg "Motoko Bootcamp V1 2022 | Motoko Variant types") ](https://www.youtube.com/watch?v=GFenqSGhj7I)

---

<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:** [December 17, 2022, 5:01pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/3 "2022-12-17T17:01:05Z")

</div>

For it to be truly unknown you’d need to use a type argument, but that means the only useful thing you can do with the value is return it:

```auto
func returnObjectOfUnknownShape<T>(arg1: T) : T {
  arg1
};

```

This is more commonly known as the “identity” function:

```auto
func id<A>(x: A) : A = x;

```

```auto
let basic = {
  example = 0.0;
  another = 2.2;
  third = {
    a = {
      b = 2;
      c = 3;
    };
    b = 2;}
};

let just_one = {
  onething = "true";
};

```

```auto
let _ = id(basic);
let _ = id(just_one);

```

---

<div class="post-metadata">

**Author:** ![blabagastered](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/blabagastered/32/8592_2.png) [@blabagastered](https://forum.dfinity.org/u/blabagastered)\
**Post date:** [December 23, 2022, 6:22pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/4 "2022-12-23T18:22:13Z")

</div>

Thank you. I did see this at the time and forgot to reply.

I was trying to make it return an object of _unknown_ shape, having done some processing on it, which appears to be impossible in Motoko. It’s ok.

---

<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:** [December 23, 2022, 10:28pm UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/5 "2022-12-23T22:28:06Z")

</div>

You can do this:

```auto
func returnObjectOfUnknownShape(arg1: Text) : {} {
  if (arg1 == "") {
    {
      basic = {
        example = 0.0;
        another = 2.2;
        third = {
          a = {
            b = 2;
            c = 3;
          };
          b = 2;
        };
      };
    };
  } else {
    {
      just_one = "true";
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [December 24, 2022, 1:23am UTC](https://forum.dfinity.org/t/how-to-define-a-function-that-returns-an-object-of-unknown-shape/17515/6 "2022-12-24T01:23:10Z")

</div>

See [GitHub - icdevs/candy\_library: Library for Converting Types and Creating Workable Motoko Collections](https://github.com/icdevs/candy_library)
