Skip to content
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

Unify structure with one that requires more fields if all of them are read only and optional. #6031

Closed
back2dos opened this issue Feb 14, 2017 · 4 comments

Comments

@back2dos
Copy link
Member

Basically I would like this to compile:

class Test {
  static function main() {
    var l:Less = { foo: 42 };
    var m:More = l;
  }
}

typedef Less = {
  var foo(default, never):Int;
}
    
typedef More = {
  @:optional var foo(default, never):Int;
  @:optional var bar(default, never):Int;    
}

The compiler tells me Less should be More but I would argue that in this case Less indeed is More ;)

@Simn
Copy link
Member

Simn commented Feb 14, 2017

If I recall correctly, @ousado has shown that this is not sound.

@back2dos
Copy link
Member Author

Upon second though I realize now that this requires special treatment on some static targets when the object is actually an instance. Right now it can just blow up: http://try.haxe.org/#Fd4A1 - that said, an additional check could be performed before accessing @:optional fields through anonymous types. That makes this a bigger deal than I initially though, but short of a counter example I still think it would be a good idea to add eventually.

@ousado
Copy link
Contributor

ousado commented Feb 15, 2017

Yes, it's not sound - the following would compile:

class Test {
  static function main() {
    var m2:More2 = {foo:1,bar:"2"};
    var l:Less = m2;
    var m:More = l;
  }
}

typedef Less = {
  var foo(default, never):Int;
}
    
typedef More = {
  @:optional var foo(default, never):Int;
  @:optional var bar(default, never):Int;    
}
typedef More2 = {
  var foo(default, never):Int;
  var bar(default, never):String;    
}

@back2dos
Copy link
Member Author

Well, I suppose it's generally a fair question whether More2 and More should unify only in one direction (going from non-optional to optional) in the first place, but I see now that my request would require quite a few changes on anonymous types. Thanks for the info ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants