# Does autocasting enums for default parameter work for alias unions?

**URL:** <https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176>\
**Category:** Offtopic\
**Created:** [May 27, 2020, 12:13am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176 "2020-05-27T00:13:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![epoch](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/epoch/32/708_2.png) [@epoch](https://forum.crystal-lang.org/u/epoch)\
**Post date:** [May 27, 2020, 12:13am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/1 "2020-05-27T00:13:09Z")

</div>

```auto
enum Auto
  Auto
end

def cell(width : Auto = :auto)
  pp width.class
end

cell()

```

this works fine. calling cell shows the type is `Auto`  
But if I use a alias union Width of Int32 and Auto instead

```auto
enum Auto
  Auto
end

alias Width = Int32 | Auto

def cell(width : Width = :auto)
  pp width.class
end

cell()

```

I get this error

```auto

 219 | def cell(width : Width = :auto)
                ^
Error: can't restrict Symbol to Width

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 27, 2020, 10:39am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/2 "2020-05-27T10:39:40Z")

</div>

As your example shows, it seems to not work for unions. That might be problematic when a union contains different enum types with conflicting members, or just plain symbols.

You can use different overloads though. For calling `cell` this is equivalent to the union type restriction.

```auto
enum Auto
  Auto
end

def cell(width : Auto = :auto)
  pp width.class
end

def cell(width : Int32)
  pp width.class
end

cell()

```

---

<div class="post-metadata">

**Author:** ![epoch](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/epoch/32/708_2.png) [@epoch](https://forum.crystal-lang.org/u/epoch)\
**Post date:** [May 28, 2020, 6:56am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/3 "2020-05-28T06:56:11Z")

</div>

Are there plans to make this work? It feels right that auto casting should work given declare the type to be `Width`.

by the way I can usually work around this problem by being explicit.

```auto
enum WidthOption
   Auto
end

alias Width = Int32 | WidthOption

def cell(width : Width = WidhtOption::Auto)
  pp width.class
end

cell()

```

This works. I just think the syntax for symbols currently is causing quiet a bit of inconsistency in Crystal.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 28, 2020, 10:30am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/4 "2020-05-28T10:30:12Z")

</div>

Interestingly, autocasting works at callsite:

```cr
enum WidthOption
   Auto
end

alias Width = Int32 | WidthOption
 
def cell(width : Width)
  pp width.class
end

cell(:auto)

```

It’s just broken for the default argument.

> [@epoch](#):
>
> I just think the syntax for symbols currently is causing quiet a bit of inconsistency in Crystal.

Unfortunately, this is true. Autocasting is a great feature but it only works to some extent. It’s not too bad I think. But eventually, I’d like to remove the symbol datatype so symbol literals can be used exclusively for enum members, which would help improve some use cases for that. But there’s no progress on that and it may actually be harder than it seems.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 28, 2020, 11:27am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/5 "2020-05-28T11:27:37Z")

</div>

Note that this works already:

```crystal
enum Auto
  Auto
end

alias Width = Int32 | Auto

def cell(width : Width)
  pp width.class
end

cell(:auto)

```

It just doesn’t work when you use it as a default value inside the method, and that’s a bug. I’ll try to fix today.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 28, 2020, 11:29am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/6 "2020-05-28T11:29:40Z")

</div>

If I don’t get to fixing it, the workaround is very simple: use Auto::Auto in the default value, but that still allows passing `:auto` in the method, which I think is the important part you were looking for?

---

<div class="post-metadata">

**Author:** ![epoch](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/epoch/32/708_2.png) [@epoch](https://forum.crystal-lang.org/u/epoch)\
**Post date:** [May 28, 2020, 11:34am UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/7 "2020-05-28T11:34:33Z")

</div>

Yes at call site it works. Which is how I got it working in the library so far. But it’s good to know that not working as default value is considered a bug.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [May 28, 2020, 12:01pm UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/8 "2020-05-28T12:01:43Z")

</div>

Fixed! [https://github.com/crystal-lang/crystal/pull/9366](https://github.com/crystal-lang/crystal/pull/9366) :-)

---

<div class="post-metadata">

**Author:** ![epoch](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/epoch/32/708_2.png) [@epoch](https://forum.crystal-lang.org/u/epoch)\
**Post date:** [May 28, 2020, 12:55pm UTC](https://forum.crystal-lang.org/t/does-autocasting-enums-for-default-parameter-work-for-alias-unions/2176/9 "2020-05-28T12:55:18Z")

</div>

That’s crazy fast!! Thanks @asterite
