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

add `using override` syntax #6654

Closed
wants to merge 4 commits into
base: development
from

Conversation

Projects
None yet
6 participants
@Simn
Copy link
Member

Simn commented Oct 9, 2017

This picks up my idea from #5271 (comment): Add a using override Path syntax which works exactly like a normal static extension, but is checked before the type's fields are checked. The implementation is accordingly straightforward.

Usage is like so:

using override Main;

class Main {
	static public function main() {
		trace("foo".charAt(0));
	}

	static function charAt(s:String, i:Int) {
		return 'myCharAt: $s.($i)';
	}
}

Generated JavaScript:

// Generated by Haxe 4.0.0
(function () { "use strict";
var Main = function() { };
Main.main = function() {
	console.log("Main.hx:5:",Main.charAt("foo",0));
};
Main.charAt = function(s,i) {
	return "myCharAt: " + s + ".(" + i + ")";
};
Main.main();
})();

Output:

Main.hx:5: myCharAt: foo.(0)

Remarks

This allows local drop-in replacements, which can be made global through import.hx. I see quite a few use-cases for this, such as having a set of null-handling overrides for standard library functions or even provide Unicode String drop-ins once we have them.

The only peculiarity is that you might want to ignore such usings, especially in a using-function itself. For this I'm checking for @:noUsing metadata at expression-level and simply skip the static extension lookup if it's active. Here's an example from the test I added:

class MyDefaultUsingClass {
	static public function charAt(s:String, i:Int) {
		return @:noUsing s.toUpperCase().charAt(i); // Use String's real charAt function
	}
}

I don't care much for the actual syntax here, but I think using default makes some sense.

@RealyUniqueName

This comment has been minimized.

Copy link
Member

RealyUniqueName commented Oct 9, 2017

Maybe a better syntax can be found? It doesn't look obvious using default means overriding own methods with static extensions.

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Oct 9, 2017

I'm open to suggestions, as I said I don't care much about the syntax.

@RealyUniqueName

This comment has been minimized.

Copy link
Member

RealyUniqueName commented Oct 9, 2017

using override Main?

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Oct 9, 2017

Sounds good to me. :)

@mastef

This comment has been minimized.

Copy link
Contributor

mastef commented Dec 3, 2017

How about allowing a specific method in the using syntax?

In this case you'd have using Main.charAt; and that would have precedence. Might be a bit more obvious usage, and would also allow the nice side-effect of only adding specific static extensions, instead of the whole class. Something that's already possible with the import syntax.

Simn added some commits Apr 17, 2018

Merge branch 'development' into using_default
# Conflicts:
#	src/typing/typer.ml

@Simn Simn changed the title add `using default` syntax add `using override` syntax Apr 17, 2018

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Apr 17, 2018

Changed to using override.

@ncannasse

This comment has been minimized.

Copy link
Member

ncannasse commented Apr 17, 2018

IMHO this goes a bit too far in language re-design. While we allow things to be easily extended, I would not especially research ways to replace the core language features/API, as it might creates Haxe "dialects" that would not longer be Haxe.

Plus, I don't like the suggested syntax :)

@Gama11

This comment has been minimized.

Copy link
Member

Gama11 commented Apr 17, 2018

That should already be possible via shadowing std lib files and/or manipulating them with macros (to a much greater extent too, I think). ;)

@ncannasse

This comment has been minimized.

Copy link
Member

ncannasse commented Apr 17, 2018

@Gama11 so let's leave it for people doing these things, we don't want to encourage it by giving them new syntax to see how ugly this can go ;)

@Simn

This comment has been minimized.

Copy link
Member

Simn commented Apr 18, 2018

You wanted something like this yourself for unicode drop-in replacement. But if you have another solution for that, I don't mind. Can't say I understand the "dialects" argument, surely macros are much worse in this regard.

@back2dos

This comment has been minimized.

Copy link
Member

back2dos commented Apr 18, 2018

This has nothing to do with language redesign. It's about giving people the possibility to provide alternative implementations, that are better suited for their use case and their requirements. It's a thing and always will be, because there's no way the stdlib will ever be able to provide implementations that are optimal for all cases. Therefore, a decent solution should be offered to create the possibility of tweaking implementation to best fit the problem at hand and shadowing is not sustainable: congratulations, not only have you improved that one method, but you will also get to bathe in the exquisite joy of maintaining the whole rest of the class you copy pasted from stdlib \o/

At the risk of this being closed because "reasons", here's a ready to use solution based on macros (with the usual limitations that macros entail): https://github.com/back2dos/no-spoon

@skial skial referenced this pull request Apr 19, 2018

Closed

Haxe Roundup 428 #497

1 of 1 task complete

@Simn Simn added this to the Design milestone Apr 20, 2018

@ncannasse

This comment has been minimized.

Copy link
Member

ncannasse commented Apr 20, 2018

Since there's already a macro for it I don't see the point of adding specific syntax.

@ncannasse ncannasse closed this Apr 20, 2018

@mastef

This comment has been minimized.

Copy link
Contributor

mastef commented Apr 20, 2018

What about using Main.charAt; syntax ( directly importing methods, not the whole class ) - where could this be suggested? Or is it possible to make with a macro?

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