# Coercion of nil

**URL:** <https://discourse.hanakai.org/t/coercion-of-nil/145>\
**Category:** proposals\
**Created:** [September 4, 2015, 12:30pm UTC](https://discourse.hanakai.org/t/coercion-of-nil/145 "2015-09-04T12:30:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leandrocp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/leandrocp/32/116_2.png) [@leandrocp](https://discourse.hanakai.org/u/leandrocp)\
**Post date:** [September 4, 2015, 12:30pm UTC](https://discourse.hanakai.org/t/coercion-of-nil/145/1 "2015-09-04T12:30:00Z")

</div>

**Summary** : `null` param should be returned as `nil`

Let me explain. Suppose I have the following params:

```
param :code, Integer
param :name, String

```

And my app is an API that receives the following JSON:

```
{
  "code": null,
  "name": null
}

```

Now let´s see how Lotus coerces the values. Currently, the default behaviour is to delegate coercion to Ruby. [Lotus/Validations](https://github.com/lotus/validations/blob/master/lib/lotus/validations/coercions.rb#L24), will call [kernel/Integer](https://github.com/lotus/utils/blob/master/lib/lotus/utils/kernel.rb#L332) and [kernel/String](https://github.com/lotus/utils/blob/master/lib/lotus/utils/kernel.rb#L665) respectively to coerce the params. That´s what happens:

**Actual result**

```
code => 0
name = ""

```

This can lead to unexpected behaviour because I would expect both empty values, in others words, to be “not present”.

**Expected result**

```
code => nil
name => nil

```

After chatting with @pascalbetz and @eduardodeoh, they agree that a `null` param should be held as `nil`.

**Proposal**

**updated** to reflect @pascalbetz comment.

Change how each coercer deals with `null` params:

```
# Lotus::Utils::Kernel.Integer(nil) # => nil
# Lotus::Utils::Kernel.Integer("23.4") # => 23
def self.Integer(arg)
    if arg.nil?
        nil
    else
        super(arg)
    # ...
end

```

And apply the same for the rest of the coercers.

---

<div class="post-metadata">

**Author:** ![pascalbetz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/pascalbetz/32/97_2.png) [@pascalbetz](https://discourse.hanakai.org/u/pascalbetz)\
**Post date:** [September 4, 2015, 12:35pm UTC](https://discourse.hanakai.org/t/coercion-of-nil/145/2 "2015-09-04T12:35:58Z")

</div>

I think that nil handling should be left up to each coercer and not wrapped away.

---

<div class="post-metadata">

**Author:** ![leandrocp](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/leandrocp/32/116_2.png) [@leandrocp](https://discourse.hanakai.org/u/leandrocp)\
**Post date:** [September 4, 2015, 12:45pm UTC](https://discourse.hanakai.org/t/coercion-of-nil/145/3 "2015-09-04T12:45:36Z")

</div>

You´re right. It´s a responsibility of each coercer to know what to do with `nil`.

---

<div class="post-metadata">

**Author:** ![eduardodeoh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.hanakai.org/eduardodeoh/32/119_2.png) [@eduardodeoh](https://discourse.hanakai.org/u/eduardodeoh)\
**Post date:** [September 4, 2015, 6:00pm UTC](https://discourse.hanakai.org/t/coercion-of-nil/145/4 "2015-09-04T18:00:08Z")

</div>

Today,

```auto
nil.public_methods.select {|e| e =~ /^to_/ }.map {|v| {"#{v}" => nil.send(v)} }

```

returns

```auto
=> [{"to_i"=>0}, {"to_f"=>0.0}, {"to_s"=>""}, {"to_a"=>[]}, {"to_h"=>{}}, {"to_r"=>(0/1)}, {"to_c"=>(0+0i)}, {"to_enum"=>#<Enumerator: ...>}]

```

So, i also agree with you and @pascalbetz.
