-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
Support named parameters at function call sites (kinda like Objective-C) #4507
Comments
Good proposal, i'm used like this from objc and swift, i forgot my own Haxe code what is calling in the meantime. This will bring some clarity to code that will become old in the future. Hope this doesn't slow down the compiler too much if implemented, because the workarounds surely do slowdown the runtime. |
I like the idea, but it seems to me that there's a consitency problem with function types: function foobar(a:Int, b:String) { ... }
var f:Int->String = foobar;
f(???); <- argument names are lost C# solves this issue quite elegantly since their delegate types have named arguments. Maybe if we allow names in function type syntax |
Fist, and most importantly, we cannot use the : syntax since it's already used in Haxe, Also, it is considered a good practice if you have named args to forbid developers from using unamed args when several of them have the same type, in order to to make sense of I think using a typedef with optional fields if required is already a quite good alternative, and it has the additional bonus of being able to pass the whole "arguments" to sub methods without forgetting any. |
I want named parameters, but I disagree with the OP's request (emphasis mine):
If they have to be fully enumerated and in order, you lose the obvious named parameter benefit of "I only want to specify 3 of 20 optional parameters, in any order, without building a typedef to do it." Just now @ncannasse's comment came in... Writing typedef's is fine, but it means I have to own the code (and change my signatures), or write wrappers around library code and re-express the default values of the signature in my wrapper. Simply stated: I want to use library functions without knowing the order or default values of the parameters. e.g. public function new (font:String = null, size:Null<Int> = null, color:Null<Int> = null, bold:Null<Bool> = null, italic:Null<Bool> = null, underline:Null<Bool> = null, url:String = null, target:String = null, align:TextFormatAlign = null, leftMargin:Null<Int> = null, rightMargin:Null<Int> = null, indent:Null<Int> = null, leading:Null<Int> = null) So I want: |
@jcward what's wrong with the wrapper? |
@ncannasse I added a link to your comment to my "awesome-haxe" link list here: https://github.com/nadako/awesome-haxe |
Let's think through the wrapper. I write: typedef TextFormatOptions = { // Moar writing
@:optional var font:String;
@:optional var size:Null<Int>;
...
}
class MyUtilClass { // Still more writing
public static function namedTextFormat(args:TextFormatOptions):TextFormat
{
// Do I have to re-express defaults here if they're non-null? e.g. Imagine size
// had a default of 12...
return new TextFormat(args.font, args.size==null ? 12 : args.size, ...);
}
} LMK if I've done anything dumb. To me, this is a ton of writing, and a custom solution that reduces portability of my code. |
But that can be auto-generated with macros |
Apparently, which is why I plan on looking at Brendon's named macro. But now you've limited "named parameters" to a small sub-set of Haxe users -- namely, those who know what macros are and how to use them. |
Well, we're talking about extern library, aren't we? An extern maker can integrate that macro in their library, I think. |
Perhaps, but now we have fragmentation / varying user experience, and new users won't understand why. Which honestly seems prevalent in Haxe, and one reason OpenFL is so popular is that @jgranick cares about the total user experience (so it's understandable that he forked hxcpp at one point -- not great for the haxe community, but great for his new users.) But I suppose that's the crux. In open source, you either share and agree, or splinter, over-reach, and duplicate effort. Sadly, agreeing is usually too hard. |
I don't mind typedefs, they're great, but don't they incur a severe performance loss on targets like cpp if used a lot? From http://old.haxe.org/manual/struct:
I guess the question there is - how much slower, approximately? |
I think one could write a macro function that analyzes "object declaration" expressions and generates code that simply passes arguments to the function call without actually creating an object. Also I wonder if this could be automatically inlined. |
Juraj posted an example with an inline helper -- I'm not sure if this provides the same level of optional parameters and default values that typedef does... Maybe if it's handled by a macro it can be worked out regardless. |
That is feasible, but I think that still suffers from the problem of function declared in the form |
BTW about that, Can we get rid of mandatory parens and define operator precedence for ternary already? @ncannasse |
@nadako that would cause some kind of syntax conflict with ternary operator |
Yes, that's why I was talking about defining operator precedence for ETypeCheck to avoid ambiguity. E.g. |
How about the |
@nadako That would as assume that you have |
https://groups.google.com/d/topic/haxelang/muY28cCQ8-g/discussion
Given a function/method signature like
we would normally call it like this:
I would additionally like to be able to name the arguments with exactly the names & order used in the signature.
I am not seeking anything else. E.g. allowing the parameters to appear in different orders is to me a future enhancement to do later on top of this basic feature request. :-) Also, that is just my guess at what the syntax could be, ideally.
The text was updated successfully, but these errors were encountered: