Crystal: Feature Idea: Auto-prefix `class` and `enum` arguments.

Created on 5 May 2018  路  3Comments  路  Source: crystal-lang/crystal

I am interested in getting a little more involved with the actual compiler and have an idea which I think would be a big improvement to the use of enums. If its thought to be a good idea I would appreciate some guidance on where it might be best to start.

Currently when one submits an enum as an argument it needs to be something like Process::Redirect::Close. It could simply be Close. The method is expecting a Process::Redirect. This produces long lines of repeated information; Process.run(...) for example.

I would like to modify the compiler to assume a prefix on a given enum or class as being that of the expected receiving kind.

Thoughts?

Most helpful comment

it can also conflict with type resolution:

enum Val
  Foo
  Bar
end

class Bar
end

def hello(value : Val)
end

def hello(bar : Bar.class)
end

hello(Bar) # hello(::Bar) or hello(::Val::Bar) ?

I'd still prefer case insensitive symbols (i.e. :redirect) thought, with the same ambiguity/conflict caveats. Because I like symbols and it would avoid visual ambiguities with types.

All 3 comments

@chris-huxtable I assume this would only work for providing literals in method args, i.e. given

enum A
  Foo
  Bar
end

def foo(enum : A)
end

you can call foo(Foo) but x = Foo; foo(Foo) wouldn't work.

If so, then yes I think this is a simple, easy to understand semantic which should be fairly easy to implement (detecting ambiguity might be hard, i.e. def foo(x : EnumA) and def foo(x : EnumB) overloads where EnumA and EnumB both have a member called Foo). You can probably extend this mechanism to numbers too (if you're expecting a UInt64, you don't have to specify 4_u64).

it can also conflict with type resolution:

enum Val
  Foo
  Bar
end

class Bar
end

def hello(value : Val)
end

def hello(bar : Bar.class)
end

hello(Bar) # hello(::Bar) or hello(::Val::Bar) ?

I'd still prefer case insensitive symbols (i.e. :redirect) thought, with the same ambiguity/conflict caveats. Because I like symbols and it would avoid visual ambiguities with types.

I'd prefer "auto prefix" using symbols for enum only.
Auto prefix for classes looks too ambiguous

Was this page helpful?
0 / 5 - 0 ratings

Related issues

asterite picture asterite  路  3Comments

costajob picture costajob  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

cjgajard picture cjgajard  路  3Comments

TechMagister picture TechMagister  路  3Comments