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

Miscellaneous additions #333

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Miscellaneous additions #333

wants to merge 3 commits into from

Conversation

Skyb0rg007
Copy link
Contributor

This PR adds some additional functions to different structures.

Description

Time.tick

This is the smallest time value. It is useful to be able to know what the smallest time unit is in code, and this addition was recommended in SMLFamily/BasisLibrary#35.

Universal.tagTryProject

It is a common usage pattern to do the following:

val myTag : t Universal.tag = ...
fun myFunc (u : Universal.universal) =
  if Universal.tagIs myTag u
    then doStuff (Universal.tagProject myTag u)
    else doOtherStuff ()

Because universal is implemented using an optional, this code performs additional checks.
It also encourages a bad pattern (check + unwrap) instead of pattern matching.
Now one can write:

val myTag : t Universal.tag = ...
fun myFunc (u : Universal.universal) =
  case Universal.tagTryProject myTag u
    of SOME x => doStuff x
     | NONE => doOtherStuff ()

Unsafe.ptrEq

This definition was referenced in #327, and it makes sense to add it to the Unsafe module to bless this as a valid implementation.

How Has This Been Tested?

Time.tick and Universal.tagTryProject are trivially tested, but these are trivial additions.
Unsafe.ptrEq has not been extensively tested.

Copy link
Contributor

@JohnReppy JohnReppy left a comment

Choose a reason for hiding this comment

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

This implementation is bogus. The size of the "tick" is set by the runtime system and may differ from OS to OS. It is also the case that maybe it should be a function that returns the smallest time quanta, instead of a Time.time value.

@Skyb0rg007
Copy link
Contributor Author

The intention of Time.tick is not for OS-level precision, but Time.time precision. As mentioned in the BasisLibrary discussion, if one is constructing Time.time values, it is important to know at what point that time value is truncated. By providing a "smallest non-zero time" value, this can be inspected.

@Skyb0rg007
Copy link
Contributor Author

This is a replacement for the code:

fun smallestTime () = let
      fun go n = if Time.zeroTime = Time.fromNanoseconds n then go (n * 10) else n
      in  go 1 end
val tick = Time.fromNanoseconds (smallestTime ())

This is Time.fromNanoseconds 1000 in SML/NJ, which is TIME { usec = 1 }.

@JohnReppy
Copy link
Contributor

I had misremembered the discussion about tick. Before putting this in the implementation, I'd like to have a Basis Library proposal for it. I'm throwing something together right now.

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