Classic inheritance principle not supported in Haxe?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Classic inheritance principle not supported in Haxe?

basvanmeurs
Hi, I'm having a situation here in which the compiler doesn't accept something that imo is perfectly legal code.

I'm having 2 packages: database and database.adodb. The first package contains the same hierarchy as the second; a classic case of separating the abstraction from the implementation.

I have a database.Connector interface, which is implemented by the database.adodb.MySqlConnector class. The interface contains a function:
public function connect() : database.Connection;

When I implement this in the MySqlConnector class, the following is allowed:
public function connect() : database.Connection {
  return new MySqlConnection(...);
}

but the following is not:
public function connect() : database.adodb.Connection {
  return new MySqlConnection(...);
}

Even though MySqlConnection extends database.Connection.

This behavior is supported in Java. I wonder why it isn't accepted by Haxe, as the compiler can be 100% sure that whatever is returned by database.adodb.Connector.connect is an instance of database.adodb.Connection, and thus database.Connection.

Any clues? Is this a shortcoming of Haxe?

Regards,
Bas
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

MarcWeber
Hi basvanmeurs,

I find it very hard following your text.

Why don't you copy paste the complete example which doesn't compile?

Maybe someone else can - thus all you have to do is waiting then.

Marc Weber

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

wvxvw
In reply to this post by basvanmeurs
I think this is rather because Java has overloading and HaXe doesn't, what you describe is essentially an overloading, when overriding you should preserve the signature. In HaXe you'd solve this by using generics, that is, if you want functions to return similar type (but not exactly the same), you'd do something like this:

class Connector<T:Connection> {

function connect():T { ... }

}

new Connector<MySqlConnection>().connect(); // will be typed to MySqlConnection

Best.

Oleg

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

basvanmeurs
Thanks, that must be it! Too bad that Haxe doesn't support overloading; I think it would be a nice feature which would help me make better designs. Don't think it's going to happen though as ECMA-based languages usually don't have overloading..
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

Renaud Bardet
Overloading means that a method can be defined more than once if each definition has different parameters,
e.g.

public function foo() : Void {}

public function foo(I : Int) : Int {}

In your case this is not overloading since your functions have the same parameters but different return types,
in fact if you tried to define these two functions in the same class it won't work because the compiler don't know wich one to call :

public function bar() : Int {}

public function bar() : Float {}

doesn't type check !


So if java compiles in your case it's not due to overloading,
Java allow to restict the return type of a redefined method to a sub-type of the original return type,
you won't redefine foo() : Float by foo():String, but you can restrict it to foo() : Int

Regards,

Renaud


2010/5/3 basvanmeurs <[hidden email]>

Thanks, that must be it! Too bad that Haxe doesn't support overloading; I
think it would be a nice feature which would help me make better designs.
Don't think it's going to happen though as ECMA-based languages usually
don't have overloading..
--
View this message in context: http://haxe.1354130.n2.nabble.com/Classic-inheritance-principle-not-supported-in-Haxe-tp4997238p4998631.html
Sent from the Haxe mailing list archive at Nabble.com.

--
haXe - an open source web programming language
http://haxe.org




--
Renaud Bardet - Developer
Succubus Interactive
[hidden email]
--

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

basvanmeurs
Thanks Renaud. Maybe in that case it would be possible for Haxe to support this, as the change wouldn't affect the output but just improve the type checking of the compiler!

I wonder if Nicolas would think this is an improvement to Haxe.
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

Nicolas Cannasse
basvanmeurs a écrit :
> Thanks Renaud. Maybe in that case it would be possible for Haxe to support
> this, as the change wouldn't affect the output but just improve the type
> checking of the compiler!
>
> I wonder if Nicolas would think this is an improvement to Haxe.

It was possible before to change the signature of a method in subclasses
  (as long as it was "compatible" with superclass one).

But the Flash9 platform (AVM2) does not support such redefinition
natively, and supporting the feature on this platform would be quite
hard, so we made the choice to remove it for all platforms.

It might be added again if we find a good way to handle it.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

gershon
If facing such a problem, I usually set the return type to the interface not the implementing class, but that might require casting the return...
Maybe another alternative (though Generics fit well here) could be a function member?

static var connect : void->Connection;
...
connect = function()  return new MySqlConnection(...);

Kinda silly maybe... another option could be using the Type\Reflection api (though, again, Generics are probably neater...)
A bit rusty on this topic, but should look kinda like:

public function connect(cl:Class<Dynamic>, args:Array<Dynamic>) {
return  createInstance(getClass(cl), args);
}

connect(MySqlConnection, []);

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

basvanmeurs
In reply to this post by Nicolas Cannasse
Hi Nicolas, that's very interesting. The OO-principles in Haxe are very exciting, especially things like the way accessor-functions are dealth with (public var example(getExample, null)) and the 'implements' on classes are amazing, especially when you have a Java background you realise the benefits of these features. This particular 'return-value-restriction' feature is not as important to me as the features just mentioned but still a good concept, and it would be great if it could be incorporated in Haxe which is a language which has exciting new OO-concepts. Vice versa, I'd love to see the features in Haxe implemented in Java, but that's a totally different matter ;-)

One more thing that interests me is why a target language can have problems with restricting methods in subclasses, especially when it is a weakly typed langauge. The type checking totally takes place in the pre-compiler and should only affect type checking. You could think of the semantic analysis of this particular feature by seperating it into 2 stages: the phase in which the types are checked and compilation errors are shown to the user when types are not respected, independent of any target language, and then 'resetting' the return type to the most general type for that method in the inheritance chain. Of course, this resetting would not be necessary as all type checking is removed in the code generation phase, but it serves to illustrate the point that it shouldn't matter. Then you end up with the same result as you would have now, when you don't respecify the return value at all. How does AVM2 make this solution impossible?

Regards,
Bas
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

Heinz Hölzer-2
Am 06.05.2010 09:58, schrieb basvanmeurs:
Hi Nicolas, that's very interesting. The OO-principles in Haxe are very
exciting, especially things like the way accessor-functions are dealth with
(public var example(getExample, null)) and the 'implements' on classes are
amazing, especially when you have a Java background you realise the benefits
of these features. This particular 'return-value-restriction' feature is not
as important to me as the features just mentioned but still a good concept,
and it would be great if it could be incorporated in Haxe which is a
language which has exciting new OO-concepts. Vice versa, I'd love to see the
features in Haxe implemented in Java, but that's a totally different matter
;-)

One more thing that interests me is why a target language can have problems
with restricting methods in subclasses, especially when it is a weakly typed
langauge. The type checking totally takes place in the pre-compiler and
should only affect type checking. You could think of the semantic analysis
of this particular feature by seperating it into 2 stages: the phase in
which the types are checked and compilation errors are shown to the user
when types are not respected, independent of any target language, and then
'resetting' the return type to the most general type for that method in the
inheritance chain. Of course, this resetting would not be necessary as all
type checking is removed in the code generation phase, but it serves to
illustrate the point that it shouldn't matter. Then you end up with the same
result as you would have now, when you don't respecify the return value at
all. How does AVM2 make this solution impossible? 

Regards,
Bas
  

Hi Bas,

yes i think this feature would be possible in as3 (using * for the types), but only at the cost of performance. It's often the question what's more
important: a language feature or the performance of the resulting application. Using strict typing in as3 results in higher performance.


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

basvanmeurs
Yes, but my point is, that the compiler can substitute the generic types in the semantic analysis phase where more restricted types have been used in subclasses/implementing classes. In this way, Haxe will be better at type checking and it will satisfy Flash's strict typing constraints.
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

Cauê W.
In reply to this post by Heinz Hölzer-2

Hi Bas,

yes i think this feature would be possible in as3 (using * for the types), but only at the cost of performance. It's often the question what's more
important: a language feature or the performance of the resulting application. Using strict typing in as3 results in higher performance.
 
Actually this is by now achieved on flash target. The generated bytecode of doSomething on both classes is MyReturn:

class Main
{
   
    static function main()
    {
        var m = new ExtendsGeneric();
        trace(m.doSomething()); 
        type(m.doSomething());      //characters 7-22 : ExtendsMyReturn
    }
   
}

class MyReturn
{
    public function new()
    {
       
    }
}

class ExtendsMyReturn extends MyReturn
{
    public function new()
    {
        super();
    }
}

class Generic<T:MyReturn>
{
    public function doSomething():T
    {
        return null;
    }
}

class ExtendsGeneric extends Generic<ExtendsMyReturn>
{
    public function new()
    {
       
    }
   
    override public function doSomething()
    {
        return new ExtendsMyReturn();
    }
}



--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Classic inheritance principle not supported in Haxe?

Nicolas Cannasse
In reply to this post by basvanmeurs
> One more thing that interests me is why a target language can have problems
> with restricting methods in subclasses, especially when it is a weakly typed
> langauge.

The bytecode verifier in Flash9 is quite strict so we need to produce
typed bytecode classes which match the verifier checks. It's not
possible to rely on * types, since you might for example subclass a
native class. In that case we need to use the same return type as the
one in superclass, and then do a dynamic downcast at every place the
function is called. Doable but not easy.

Hope that helps,

Nicolas

--
haXe - an open source web programming language
http://haxe.org