-
Notifications
You must be signed in to change notification settings - Fork 37
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
Can properties not have a default value? #376
Comments
Worth thinking about in the context of #356. If we extended |
Not sure that's quite right — I think it's probably better for Things to consider:
|
Would be nice to have to have a way to have a default value of "the current time". |
One solution could be to change the signature of the class constructor we build, so that we use the user provided For example: new_class("foo", properties = list(
time = new_property(default = quote(Sys.time()))
)) could return a function with signature: function(time = Sys.time()) while new_class("foo", properties = list(
time = new_property(default = Sys.time())
)) would return a function with signature: function(time = structure(1702317376.61459, class = c("POSIXct", "POSIXt"))) |
@t-kalinowski yeah, I was thinking that too. And maybe we don't have to worry too much about backward compatibility because most simple values are the same when you quote them (e.g. |
One to think about when it comes to defaults is the notion of a class prototype. S4 has this feature and it is quite powerful. By constructing a prototype, in the case of S7 from the property defaults, and then allowing the constructor to override those values, we would avoid some problems. For example, Construction would follow this algorithm:
This approach is still compatible with having the defaults in the formals of the constructor. That's great for making the constructor self-documenting. Step 4 would just set all of the properties again. We could avoid setting the ones that are missing, if we really wanted to prematurely optimize. |
To expand on prototype formation, there should be some effort towards a default default value, probably by calling the constructor of the property's class without arguments. If the no-arg constructor call fails, then we could just use the current default default, The methods package uses Commenting on the original idea of a constructor requiring an argument, I would leave that to user-specified constructors. The default constructor should just rely on validation for whether the object was properly constructed. |
Another problem with setting the defaults, instead of forming a prototype from them, is in the example of NOTE: this only happens on the If we do move forward with the constructor having property defaults as its argument defaults, then this problem would return, since we would be setting every property. Two ways around this:
|
Bug motivating inlining the defaults: Foo <- new_class("Foo", properties = list(x = class_any))
Bar <- new_class("Bar", Foo, properties = list(x = class_list))
Bar()
# Error: <Bar> object properties are invalid:
# - @x must be <list>, not <NULL> |
In the development version of S7, it's currently possible to make a property "required" by setting the default value to an error call: x = new_property(default = quote(stop("`x` is required"))) |
Great discussion. Closing as we’ve reached a workable solution for the next release. In S7, you can create a “required” property using a quoted Passing a missing argument (i.e., Similarly, a lazy default of |
Related to #139.
Now that properties have a default value in the constructor by default, is there a simple way to not have a default value for a property in the constructor?
It could be nice to be able to do something like
new_property(default = missing_arg())
ornew_property(required = TRUE)
.The text was updated successfully, but these errors were encountered: