Giter VIP home page Giter VIP logo

Comments (6)

moodymudskipper avatar moodymudskipper commented on June 22, 2024 1

Minimal example and comments inline :

time <- as.POSIXct("2022-01-01 00:00:00", "UTC")
constructive::construct(time, max_atomic = 0)
#> as.POSIXct(character(0), tz = character(0))

# Fails because we need a TZ
as.POSIXct(character(0), tz = character(0))
#> Error in if (nzchar(tz)) attr(res, "tzone") <- tz: argument is of length zero

# For a prototype we might expect
as.POSIXct(character(0), tz = "UTC")
#> POSIXct of length 0
identical(as.POSIXct(character(0), tz = "UTC"), vctrs::vec_ptype(time))
#> [1] TRUE

# But that shows the definitions of a prototype and of max_atomic = 0 might be different
# Is this the right max_atomic = 0 version ?
as.POSIXct(character(0)) |>
  structure(tzone = character(0))
#> POSIXct of length 0

# or can we say that we can't guarante code to run when max_atomic is set (because we can't)
# and not go through a rabbit hole of exceptions ? 

# Also what about objects with a class and no implemented constructor ? 
x <- 1
class(x) <- "foo"
constructive::construct(x, max_atomic = 0)
#> numeric(0) |>
#>   structure(class = character(0))

In the general case we can't make an exception for class and attributes, because they can be complex (classes themselves can have attributes), so the latter probably has to stay this way. But's important to know that while the standard use enforces equivalence of original and constructed objects, max_atomic trims it in a way that is dependent on the implemented constructors, and thus the output might change as the package gets more constructors.

A corollary is that prototypes are defined by arbitrary rules, or at least rules
that I haven't formalized yet (Since the rule "everything" length 0 is not accurate), so if we want to construct a prototype, use vctrs::vec_ptype() first (it made and knows the rules), then construct in a standard way.


TLDR :

  • We can't say max_atomic = 0 means prototype
  • The correct way to handle a POSIXct with max_atomic = 0 might be as.POSIXct(character(0)) |> structure(tzone = character(0))
  • OR the current way might be right, but must be better documented.
  • If we need to construct a prototype we should use constructive::construct(vctrs::vec_ptype(data)) without max_atomic arg (@andreranza that's the part for you)

from constructive.

moodymudskipper avatar moodymudskipper commented on June 22, 2024 1

Thanks!

Just to recall, from dput_small1() you got as.POSIXct(character(0)) |> structure(tzone = character(0)) when dealing with POSIXct, so at that time you might had this representation in mind (?).

I think it was just using calling dput on zero length elements

Just for curiosity, is there a theoretical reason for which you would tend to avoid as.POSIXct(character(0)) |> structure(tzone = character(0))?

Because I think max_atomic's role might be just to show less, not to change the way it's shown. The core feature is not to modify the input but to represent it. I think it's something I oversaw when starting to talk about building prototypes with this package and it's probably a mistake.
In the code max_atomic is only leveraged when attempting to construct atomic types, the rest of the skeleton is built from the real input.

Also if we did this for POSIXct we'd need to consider exceptions for all constructors to try to build something that works from zero length vectors, and for some it might be impossible anyway.

I see the point that {vctrs} seems to have a different opinion on actually "what is a prototype for a POSIXct". What I see is that for {vctrs} a valid time zone should be provided. And, there might actually be a valid "theoretical" reason that underpins this "choice" that I am not aware of, or ignore (btw if there is a theoretical reason there this not even a "choice", but just a fact that follows, right?).
So, I cannot see, due to my illiteracy on this topic, what is the degree of "subjectivity" involved in the definition of a "valid prototype".

I think by default vctrs::vec_type() reduces the vec_size to zero (approx same as length() except for data frames where it's now()) but keeps attributes intact, in this case it's a simple rule, but vec_ptype is a generic so any more sophisticated behaviours can be implemented.

So construct(vec_ptype()) is the proper idiom if we want a prototype to use with vec_assert() or vec_cast()

Maybe the two latter functions in the end just check names, class and attributes though.

As far as I am concerned, as.POSIXct(character(0)) |> structure(tzone = character(0)) would be a valid prototype. I don't see the tzone necessary. But this is just my "illiterate" opinion 😅 driven but the following mere "practical" reason:

as.POSIXct(character(0)) |> structure(tzone = character(0)) runs on the console, whereas as.POSIXct(character(0), tz = character(0)) not.

I don't say as.POSIXct(character(0), tz = character(0)) is a prototype, it's not.
It's the short representation of a POSIXct vector with a non NULL timezone however. And short representations don't have to run they need to have the same call structure as the complete representation and be shorter.

We will dissociate completely the concept of max_atomic = 0 with the concept of prototype.

from constructive.

moodymudskipper avatar moodymudskipper commented on June 22, 2024

If we don't change the code construction, which I currently tend to believe we shouldn't, it might still be good to try by default to run the code, even if check is FALSE, and warn the user when we output failing code.

from constructive.

andreranza avatar andreranza commented on June 22, 2024

Thanks!

Just to recall, from dput_small1() you got as.POSIXct(character(0)) |> structure(tzone = character(0)) when dealing with POSIXct, so at that time you might had this representation in mind (?).

Just for curiosity, is there a theoretical reason for which you would tend to avoid as.POSIXct(character(0)) |> structure(tzone = character(0))?

I see the point that {vctrs} seems to have a different opinion on actually "what is a prototype for a POSIXct". What I see is that for {vctrs} a valid time zone should be provided. And, there might actually be a valid "theoretical" reason that underpins this "choice" that I am not aware of, or ignore (btw if there is a theoretical reason there this not even a "choice", but just a fact that follows, right?).

So, I cannot see, due to my illiteracy on this topic, what is the degree of "subjectivity" involved in the definition of a "valid prototype".

As far as I am concerned, as.POSIXct(character(0)) |> structure(tzone = character(0)) would be a valid prototype. I don't see the tzone necessary. But this is just my "illiterate" opinion 😅 driven but the following mere "practical" reason:

as.POSIXct(character(0)) |> structure(tzone = character(0)) runs on the console, whereas as.POSIXct(character(0), tz = character(0)) not.

from constructive.

moodymudskipper avatar moodymudskipper commented on June 22, 2024

Let's deal with this in #19

from constructive.

github-actions avatar github-actions commented on June 22, 2024

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

from constructive.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.