Skip to content
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

Don't inline classes in constructor if possible #481

Merged
merged 14 commits into from
Nov 4, 2024

Conversation

t-kalinowski
Copy link
Member

@t-kalinowski t-kalinowski commented Oct 25, 2024

This PR changes a constructor default formal value from an inlined class call, to a call like pkgname::classname(), if class@package is not NULL.

closes #477

With this PR:

library(S7)
Coordinate <- new_class(
  "Coordinate",
  properties = list(
    x = class_double,
    y = class_double
  ),
  package = "draw"
)

Shape <- new_class(
  "Shape",
  properties = list(
    position = Coordinate, # Coordinate would normally be inlined 
    name = class_character
  ),
  package = "draw"
)

## Shape and Coordinate are from the same package, 
## so Coordinate() call is not namespaced with :: 
str(formals(Shape))
#> Dotted pair list of 2
#>  $ position: language Coordinate()
#>  $ name    : chr(0)

stopifnot(identical(
  formals(Shape)$position,
  quote(Coordinate())
))

## different package name, now Coordinate() is inlined as a namespaced :: call
Shape2 <- new_class(
  "Shape",
  properties = list(
    position = Coordinate, # Coordinate would normally be inlined 
    name = class_character
  ),
  package = "draw2"
)

str(formals(Shape2))
#> Dotted pair list of 2
#>  $ position: language draw::Coordinate()
#>  $ name    : chr(0)

stopifnot(identical(
  formals(Shape2)$position,
  quote(draw::Coordinate())
))

@t-kalinowski
Copy link
Member Author

t-kalinowski commented Oct 25, 2024

This approach of using pkgname::classname() might not work, because new_class() is expected to run at package build time, before package exports are fully known (i.e., before a call like :: or getNamespaceExports() or getExportedValue() will work correctly).

We might have to change the parent environment of the constructor to the environment where new_class() is called. Though, that also brings challenges even in S7 itself, like:

** testing if installed package can be loaded from temporary location
Error: package or namespace load failed for ‘S7’:
 .onLoad failed in loadNamespace() for 'S7', details:
  call: NULL
  error: 'new_object' is not an exported object from 'namespace:S7'
Error: loading failed

@lawremi
Copy link
Collaborator

lawremi commented Oct 27, 2024

Qualifying calls by the namespace should only be necessary when referencing class from another package. Would avoiding unnecessary qualification resolve the issue?

@hadley
Copy link
Member

hadley commented Oct 28, 2024

This is starting to feel related to #341

@t-kalinowski
Copy link
Member Author

OOPWG notes: This should be fixed and merged before release.

@t-kalinowski t-kalinowski marked this pull request as ready for review October 30, 2024 00:44
@t-kalinowski t-kalinowski requested a review from hadley October 30, 2024 00:44
R/class-spec.R Show resolved Hide resolved
R/class-spec.R Show resolved Hide resolved
R/constructor.R Show resolved Hide resolved
R/constructor.R Show resolved Hide resolved
tests/testthat/test-constructor.R Outdated Show resolved Hide resolved
R/class-spec.R Show resolved Hide resolved
tests/testthat/test-constructor.R Outdated Show resolved Hide resolved
tests/testthat/_snaps/external-generic.md Outdated Show resolved Hide resolved
@t-kalinowski t-kalinowski merged commit 4ced878 into main Nov 4, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

inlining obfucscates constructor defaults
3 participants