named-parameters

Can named arguments be used with Python enums?

核能气质少年 提交于 2019-12-06 17:01:14
问题 Example: class Planet(Enum): MERCURY = (mass: 3.303e+23, radius: 2.4397e6) def __init__(self, mass, radius): self.mass = mass # in kilograms self.radius = radius # in meters Ref: https://docs.python.org/3/library/enum.html#planet Why do I want to do this? If there are a few primitive types (int, bool) in the constructor list, it would be nice to used named arguments. 回答1: While you can't use named arguments the way you describe with enums, you can get a similar effect with a namedtuple mixin:

C++ named arguments implementation with derived classes

吃可爱长大的小学妹 提交于 2019-12-05 19:45:15
I'm trying to create a named-arguments-like constructor for some of the classes of a project. The way I'm doing it is by defining a class proxy which will hold the arguments and pass an instance of this proxy to the constructor of my classes. Everything worked fine until I had to derive one of my classes. Basically I thought: I'm gonna derive the new derived class proxy from the base class proxy. This works too, but only if I use only the derived proxy class arguments. Here is an example since it is easier to understand: class Person { public: class PersonArgs { public: const std::string& Name

Value classes introduce unwanted public methods

大城市里の小女人 提交于 2019-12-05 11:35:03
问题 Looking at some scala-docs of my libraries, it appeared to me that there is some unwanted noise from value classes. For example: implicit class RichInt(val i: Int) extends AnyVal { def squared = i * i } This introduces an unwanted symbol i : 4.i // arghh.... That stuff appears both in the scala docs and in the IDE auto completion which is really not good. So... any ideas of how to mitigate this problem? I mean you can use RichInt(val self: Int) but that doesn't make it any better ( 4.self ,

Can named arguments be used with Python enums?

久未见 提交于 2019-12-05 00:04:36
Example: class Planet(Enum): MERCURY = (mass: 3.303e+23, radius: 2.4397e6) def __init__(self, mass, radius): self.mass = mass # in kilograms self.radius = radius # in meters Ref: https://docs.python.org/3/library/enum.html#planet Why do I want to do this? If there are a few primitive types (int, bool) in the constructor list, it would be nice to used named arguments. While you can't use named arguments the way you describe with enums, you can get a similar effect with a namedtuple mixin: from collections import namedtuple from enum import Enum Body = namedtuple("Body", ["mass", "radius"])

How do you curry the 2nd (or 3rd, 4th, …) parameter in F# or any functional language?

亡梦爱人 提交于 2019-12-04 00:17:33
I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order? Typically you just use a lambda: fun x y z -> f x y 42 is a function like 'f' but with the third parameter bound to 42. You can also use combinators (like someone mentioned Haskell's "flip" in a comment), which reorder arguments, but I

How to define named Parameters C#

纵然是瞬间 提交于 2019-12-03 23:33:23
问题 This seems like a simple question, but for some reason I can't find the answer anywhere. Basically, I'd like to be able to implement a constructor that takes NamedParameters . By named parameters, I do not mean parameters with default values (optional parameters) such as: public SomeMethod(){ string newBar = Foo(bar2 : "customBar2"); } public string Foo(string bar1 = "bar1", bar2 = "bar2" ){ //... } A good example of what I'm trying to achieve is the AuthorizeAttribute from the System.Web.Mvc

Value classes introduce unwanted public methods

坚强是说给别人听的谎言 提交于 2019-12-03 23:21:47
Looking at some scala-docs of my libraries, it appeared to me that there is some unwanted noise from value classes. For example: implicit class RichInt(val i: Int) extends AnyVal { def squared = i * i } This introduces an unwanted symbol i : 4.i // arghh.... That stuff appears both in the scala docs and in the IDE auto completion which is really not good. So... any ideas of how to mitigate this problem? I mean you can use RichInt(val self: Int) but that doesn't make it any better ( 4.self , wth?) EDIT : In the following example, does the compiler erase the intermediate object, or not? import

named parameters with default values in groovy

浪尽此生 提交于 2019-12-03 22:16:39
Is it possible to have named parameters with default values in groovy? My plan is to make a sort of object factory, which can be called with no arguments at all in order to get an object with default values. Also, I'd need the functionality to explicitly set any of the params for the object. I believe this is possible with Python keyword arguments, for example. The code I'm attempting with right now is something like below // Factory method def createFoo( name='John Doe', age=51, address='High Street 11') { return new Foo( name, age, address ) } // Calls Foo foo1 = createFoo() // Create Foo

Better Way To Use C++ Named Parameter Idiom?

巧了我就是萌 提交于 2019-12-03 13:15:48
I've been developing a GUI library for Windows (as a personal side project, no aspirations of usefulness). For my main window class, I've set up a hierarchy of option classes (using the Named Parameter Idiom ), because some options are shared and others are specific to particular types of windows (like dialogs). The way the Named Parameter Idiom works, the functions of the parameter class have to return the object they're called on. The problem is that, in the hierarchy, each one has to be a different class -- the createWindowOpts class for standard windows, the createDialogOpts class for

How can I pass named arguments to a Rake task?

别来无恙 提交于 2019-12-03 09:34:16
Is there a way to pass named arguments to a Rake task without using environment variables? I am aware that Rake tasks can accept arguments in two formats: Environment Variables $ rake my_task foo=bar This creates an environment variable with the name foo and the value bar that can be accessed in the Rake task my_task by ENV['foo'] . Rake Task Arguments $ rake my_task['foo','bar'] This passes the values foo and bar to the first two task arguments (if they are defined). If my_task were defined as: task :my_task, :argument_1, :argument_2 then argument_1 would have the value foo and argument_2