allow variance of property types?

27 views
Skip to first unread message

Raoul Duke

unread,
Aug 24, 2015, 2:00:46 AM8/24/15
to haxe...@googlegroups.com
(pseudocode)
class Base {}
class Sub extends Base {}
interface Foo { var x(get,null):Base; }
class Foo { public var x(get,null):Sub; } // i think i wish this were
not a compilation error.

Level1.hx:8: lines 8-31 : Field scene has different type than in ILevel
Level1.hx:8: lines 8-31 : com.haxepunk.Scene should be Scene1

p.s. i am pretty sure that i always have and always will think that
last error message is backwards.

Juraj Kirchheim

unread,
Aug 24, 2015, 2:17:00 AM8/24/15
to haxe...@googlegroups.com
Assume this compiles:

  class Base {}
  class Sub extends Base {}

  interface IFoo { var x(get, set):Base; }
  class Foo implements IFoo { public var x(get, set):Sub; } 

Then this can happen:

  var foo = new Foo();
  var ifoo:IFoo = foo;
  ifoo.x = new Base();//ouch!

If anything, this could only work on readonly properties. In fact it does already for anonymous types: http://try.haxe.org/#210ee
Generally, nominal types are not too good with variance in Haxe. I don't quite know why, but I think that several target runtime don't really allow for it to start with. Haxe already does quite a dance to allow for variance on methods.

Best,
Juraj


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Raoul Duke

unread,
Aug 24, 2015, 2:25:34 AM8/24/15
to haxe...@googlegroups.com
hi,

> If anything, this could only work on readonly properties.

Thanks for the reminder. Probably ever time I hit this, I think I
maybe get re-told that immutability as the default is one way to get
this? I mean in some other language that starts off with immutability
as the main law of the land.

I mean, in my case right now, I'd be happy with it being read only
from the outside; see how my pseudo code had null in it.

software is hard.
Reply all
Reply to author
Forward
0 new messages