Open t-kalinowski opened 2 months ago
A few of my own thoughts are below, from when I was thinking about this myself:
Possibly the declarations should be one level deeper for clarity of what we're actually declaring, though I guess this depends on if the intention is for declare()
to be used for other things too (e.g. evaluation semantics).
declare(type(a = integer(1)))
Allowing for literals would be good:
declare(type(a = "abc" | 4 | 5 | FALSE))
Creating new named types from e.g. type unions would also be good. In this example I use <-
rather than =
to define a new type instead of asserting a type for a variable:
declare(type(my_type <- integer(5) | FALSE ))
declare(type(a = my_type))
Also, type generics would be really nice. Here, I use ->
to indicate a type parameter U
.
declare(type(maybe <- (U -> U | FALSE)))
# declare(type(bar = integer(1) | FALSE))
declare(type(bar = maybe(integer(1))))
One can imagine a combination of type generics and function definitions:
declare(type(
wrapper <- (U -> function(U) { list(U) })
))
declare(type(
fn = wrapper(integer(1))
))
fn <- function(x) {
list(x + 1L)
}
Also, should other attributes be possible to declare?
declare(type(list(3), names = c("abc", "def", "ghi"), class = "myclass"))
Do you imagine one call like declare(type(...))
per symbol? That seems like a lot of syntax is required.
What do you think of:
declare(
name1 = type(...),
name2 = type(...),
...,
return = type(...)
)
Regarding the last question about attributes, I think it's a good idea. This could be supported with this approach too, like:
declare(
time = type(double(), class = "POSIXct", tz = NULL | character(1)),
name2 = type(...),
name3 = type(...)
)
Ideally, all this would work nicely with S7, so one could do:
declare(
time = type(S7::class_POSIXct)
)
The class
attribute does raise some interesting questions. Would it behave like inherits()
and check for the existence of that string in the class vector, ignoring other classes, or would it do a strict check using identical()
? 🤔
It would be nice to use
declare()
for type hints, following the style of a Fortran subroutine type manifest. These type declarations could then:Example syntax:
Some additional, more experimental syntax could also be supported:
NULL
able) args:Named dimensions: For multidimensional arrays, allow naming dimensions for clarity :
Value constraints: