Proposal for a better using by applying the most specific function available

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

Proposal for a better using by applying the most specific function available

Heinz Hölzer-2
Hi,

let's say i have two classes with extensions:

class A {
    public static function toString <T>(v:T):String {
        return Std.string(v);
    }
}

class B {
   
public static function toString <T>(v:Array<T>):String {
        return "Array " + Std.string(v);
    }

}

if i use these classes like:

using A;
using B;

class C {
   
public static function main ():String {
        [1,2].toString();
    }

}

the function toString of class A is used, because "using A" is declared before "using B".
I think it would be a lot better if the most specific version (in this case B.toString) would be used before falling back to order of declaration.

Especially when working with collections it's nice to have a fallback version for Iterable, when a version for array is not available. Currently it's only possible
to achieve this by explicitly "using" the "special version" before the "fallback version" like:

using ArrayTools;
using IterableTools;


Any opinions?

best,
h


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

Re: Proposal for a better using by applying the most specific function available

Heinz Hölzer-2
I would also like to see that the same applies for typedefs, so that

typedef MyString = String;

class A {
   
public static function toString (v:MyString):String {
        return Std.string(v);
    }
}

takes precedence over

class A {
   
public static function toString (v:String):String {
        return Std.string(v);
    }
}

best,
h


Am 11.11.2011 14:43, schrieb Heinz Hölzer:
Hi,

let's say i have two classes with extensions:

class A {
    public static function toString <T>(v:T):String {
        return Std.string(v);
    }
}

class B {
   
public static function toString <T>(v:Array<T>):String {
        return "Array " + Std.string(v);
    }

}

if i use these classes like:

using A;
using B;

class C {
   
public static function main ():String {
        [1,2].toString();
    }

}

the function toString of class A is used, because "using A" is declared before "using B".
I think it would be a lot better if the most specific version (in this case B.toString) would be used before falling back to order of declaration.

Especially when working with collections it's nice to have a fallback version for Iterable, when a version for array is not available. Currently it's only possible
to achieve this by explicitly "using" the "special version" before the "fallback version" like:

using ArrayTools;
using IterableTools;


Any opinions?

best,
h


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

Re: Proposal for a better using by applying the most specific function available

sledorze
In reply to this post by Heinz Hölzer-2
I would agree with you.

To bring something into the discussion; here's some food for though:
Relying on 'using' order gives some control over it..
If we choose the most specific type version, how may one control that? (shadowing?)
..and is it desirable..?
Reply | Threaded
Open this post in threaded view
|

Re: Proposal for a better using by applying the most specific function available

Heinz Hölzer-2
The most specific version allows overloading and this is powerful.
I would also love to see more control for using together with this
feature. Something like:

using Stringtools.trim;

using Stringtools excluding trim;

etc.






Am 11.11.2011 17:18, schrieb sledorze:

> I would agree with you.
>
> To bring something into the discussion; here's some food for though:
> Relying on 'using' order gives some control over it..
> If we choose the most specific type version, how may one control that?
> (shadowing?)
> ..and is it desirable..?
>
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/Proposal-for-a-better-using-by-applying-the-most-specific-function-available-tp6985398p6985883.html
> Sent from the Haxe mailing list archive at Nabble.com.
>


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

Re: Proposal for a better using by applying the most specific function available

sledorze
Yes, and in case of same types, ordering of using being relevant but issuing a warning.

Reply | Threaded
Open this post in threaded view
|

Re: Proposal for a better using by applying the most specific function available

Nicolas Cannasse
In reply to this post by Heinz Hölzer-2
[...]
> I think it would be a lot better if the most specific version (in this
> case B.toString) would be used before falling back to order of declaration.

It might be interesting but is not an easy task and I think there's
other priorities right now such as most of the things listed there :
http://code.google.com/p/haxe/issues

Best,
Nicolas

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