New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value for enum parameters #27

Open
sebthom opened this Issue Aug 24, 2017 · 9 comments

Comments

Projects
None yet
7 participants
@sebthom
Copy link

sebthom commented Aug 24, 2017

I was surprised to find that while

enum Schedule {
    Hourly(?minute:Int);
    // ...
}

is possible, this is not:

enum Schedule {
    Hourly(minute:Int=1); // characters 18-19 : Unexpected =
    // ...
}

Right now I have to do additional null-checks on enums with optional parameters like so

var instance = new MyClass(Schedule.Hourly());

function MyClass {
    var schedule:Schedule;

    public function new(var schedule:Schedule){
       // validate schedule
       switch (schedule) {
           case Hourly(minute):
                // check for null and apply a default value if found
                if(minute == null) {
                   minute = 1;
                   schedule = Schedule.Hourly(minute); 
                } else if (minute < 0 || minute > 59)  
                   throw "Hourly.Minute must be >= 0 and < 60";
                }
            // ...
       }
       this.schedule = schedule;
    }
}

instead of simply:

var instance = new MyClass(Schedule.Hourly());

function MyClass {
    var schedule:Schedule;

    public function new(var schedule:Schedule) {
       // validate schedule
       switch (schedule) {
           case Hourly(minute):
                if (minute < 0 || minute > 59)  
                    throw "Hourly.Minute must be >= 0 and < 60";
            // ...
       }
       this.schedule = schedule;
    }
}

Therefore I would request adding default value behaviour for enums as specified here https://haxe.org/manual/types-function-default-values.html

@delahee

This comment has been minimized.

Copy link

delahee commented Aug 24, 2017

I like this, every time we strip null possibilities from core language is a big win and lead to leaner code. +1

@nadako

This comment has been minimized.

Copy link
Member

nadako commented Aug 24, 2017

Since enum ctors with args are actually functions, maybe we can support this easily?

@RealyUniqueName

This comment has been minimized.

Copy link
Member

RealyUniqueName commented Aug 24, 2017

I'd like to see this implemented too.

@skial skial referenced this issue Aug 31, 2017

Closed

Haxe Roundup 398 #425

@fullofcaffeine

This comment has been minimized.

Copy link

fullofcaffeine commented Sep 6, 2017

+1 @sebthom You could use tink_lang to relieve the pain for now: https://haxetink.github.io/tink_lang/#/declaration-sugar/complex-default-arguments.

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Apr 17, 2018

I don't think this could be implemented without significant changes to how we handle default values. In our implementation, the default values are not part of the function type, but of the function itself. Enum constructors to not have actual functions, they are only typed as such. This would only work if we did call-site replacements, but we don't.

The only alternative is to respect this in every generator.

@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Sep 1, 2018

Enum constructors to not have actual functions, they are only typed as such.

On all platforms I've checked, they are actual functions. What's more, the type system also treats them as such, e.g. Some is of type Unknown<0> -> haxe.ds.Option<Unknown<0>>, meaning there must be a physical function somewhere and its implementation could treat default values accordingly.

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Sep 3, 2018

Look, I'm not making this up to annoy you or avoid implementing this feature. You can look at haxe/macro/Type.hx and verify for yourself that the only place default values show up is TFunc, and the only place that shows up is the TFunction expression. You will also find that EnumField only has a Type, not an expression. Therefore, adding default values to enum fields would require changes to our enum field representation.

@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Sep 3, 2018

Sorry, I misunderstood. We're in agreement after all: if the default values are actually known, there is no problem.

You can look at haxe/macro/Type.hx and verify for yourself that the only place default values show up [emphasis added] is TFunc [...]

Regrettably, upon following your invitation I failed to verify your claim. As it turns out you already introduced a compile time representation of default values. It's not a pretty one, but it is sufficient and could be used for enums just as well. Just saying ...

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Sep 3, 2018

I added that because we wanted to have the original syntax for generating documentation. For generating output, we need a properly typed representation and we cannot store that in metadata. I also wouldn't want to rely on such a hack to generate code in the first place. So the only option is change our representation of enum fields.

I'm not saying it can't be done, but I guarantee you it's more involved than you make it seem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment