-
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
Should properties have a default value? #139
Comments
This makes sense to me. The methods package initializes slots by just calling |
This means we'd need to add constructors for non-vector base types (e.g. function), and s3_factor <- s3_class("factor", factor)
s3_data_frame <- s3_class("data.frame", data.frame)
# Needed because S3 functions error when called without arguments
s3_ordered <- s3_class(c("factor", "ordered"), function(x = character(), ...) ordered(x, ...))
s3_date <- s3_class("Date", function(x = integer()) .Date(x)) If your class defines a validator and it fails with the default property values, it'd be your responsibility to either adjust the property defaults or override the constructor. Should we make the default a function? It seems like it would be convenient to do something like this: new_property("created_at", s3_date, default = function() Sys.Date()) |
For unions, the methods package uses the prototype of the first member of the union (where "NULL" is always considered first if present). In my experience, it's a lot more common to want a constant as the default, so a function might be too complex. It's fairly straightforward to achieve the same effect in the class constructor. That has the minor added benefit of putting all code executed during object instantiation in one place. |
Makes sense. What should |
It would be |
Ah you're thinking the behaviour is conditional on whether or not the argument is |
Well the default value of |
Somewhat related to this discussion, but in the course of working on this code I realised that the R7 wrapper of the environment base class used |
This would be used by the default constructor when the property is not supplied.
If properties can have a default value, should it be supplied automatically? i.e. if the property is an R7 or S4 class, it could call the default constructor. This would have the advantage that you can always easily initialise a class>
The text was updated successfully, but these errors were encountered: