Skip to content

Add typed_function() for runtime-typed function signatures#6

Merged
feddelegrand7 merged 2 commits intomainfrom
copilot/add-function-type-annotations
Mar 13, 2026
Merged

Add typed_function() for runtime-typed function signatures#6
feddelegrand7 merged 2 commits intomainfrom
copilot/add-function-type-annotations

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

sicher had no way to attach type annotations to function parameters or return values — only variables could be typed. This adds typed_function() as the function counterpart to %:% / %<-%.

API

add <- typed_function(
  function(x, y) x + y,
  params  = list(x = Numeric, y = Numeric),
  .return = Numeric
)

add(1, 2)     # → 3
add("a", 2)   # → Error: Type error in 'x': Expected numeric, got string

All existing type constructs work in params and .return:

describe <- typed_function(
  function(id, label = NULL) paste(label, id),
  params  = list(id = String | Numeric, label = Optional(String)),
  .return = String
)

Changes

  • R/main.Rtyped_function(fn, params = list(), .return = NULL):
    • Validates params entries and .return at construction time
    • Wrapper uses match.call() + eval(parent.frame()) so argument expressions are evaluated in the caller's scope; delegates via do.call(fn, evaled)
    • Default parameter values work naturally — only explicitly supplied args are type-checked
    • Tags wrapper with S3 class sicher_typed_function; stores params / return_type as attributes
  • R/main.Rprint.sicher_typed_function: renders signature as <typed_function (x: numeric, y: numeric): numeric>
  • NAMESPACE — exports typed_function, registers S3 print method
  • tests/testthat/test-sicher.R — 8 new tests covering param checks, return type, union/optional params, construction errors, and print output

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cloud.r-project.org
    • Triggering command: /usr/lib/R/bin/exec/R /usr/lib/R/bin/exec/R --no-echo --no-restore -e .libPaths(&#39;~/R/library&#39;);~&#43;~install.packages(c(&#39;devtools&#39;,&#39;testthat&#39;,&#39;glue&#39;),~&#43;~repos=&#39;REDACTED&#39;,~&#43;~lib=&#39;~/R/library&#39;,~&#43;~quiet=TRUE) d64.deb (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Co-authored-by: feddelegrand7 <28841210+feddelegrand7@users.noreply.github.com>
Copilot AI changed the title [WIP] Add type annotations for functions Add typed_function() for runtime-typed function signatures Mar 13, 2026
Copilot AI requested a review from feddelegrand7 March 13, 2026 16:05
@feddelegrand7
Copy link
Owner

perfekt, now update the readme accordingly and the index.qmd

Copy link
Owner

@feddelegrand7 feddelegrand7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update the readme and index.qmd accordingly

@feddelegrand7 feddelegrand7 marked this pull request as ready for review March 13, 2026 16:09
@feddelegrand7 feddelegrand7 merged commit 432f1a3 into main Mar 13, 2026
1 check failed
Copilot AI requested a review from feddelegrand7 March 13, 2026 16:10
Copilot stopped work on behalf of feddelegrand7 due to an error March 13, 2026 16:10
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.

2 participants