Skip to content

Support string parameters for methods #17

@spyoungtech

Description

@spyoungtech

The methods in Tempus are typically terse about the input type parameter.

For example:

tempus.ahk/tempus.ahk

Lines 2335 to 2342 in fd8112c

checked_add(other) {
out_date := Buffer(A_PtrSize)
if (other is Span) {
retcode := DllCall("tempus_ahk\date_checked_add_span", "Ptr", this.pointer, "Ptr", other.pointer, "Ptr", out_date, "Int64")
} else if (other is SignedDuration) {
retcode := DllCall("tempus_ahk\date_checked_add_signed_duration", "Ptr", this.pointer, "Ptr", other.pointer, "Ptr", out_date, "Int64")
} else {
throw Error("Unsupported type. Must be Span or SignedDuration", -2)

However, the ergonomics of the library may be improved if string arguments could be accepted where the desired type can be parsed from a string.

For example:

#Include "tempus.ahk"

ts := Timestamp.now()
span1 := Span.new().hours(2).minutes(30)

ts.checked_add(span1)

Could become:

#Include "tempus.ahk"

ts := Timestamp.now()
ts.checked_add("2h 30m")

Using the same example as above, this could perhaps be implemented roughly as so:

 checked_add(other) { 
     out_date := Buffer(A_PtrSize) 
     if (other is Span) { 
         retcode := DllCall("tempus_ahk\date_checked_add_span", "Ptr", this.pointer, "Ptr", other.pointer, "Ptr", out_date, "Int64") 
     } else if (Type(other) == "String") {
        ; actual implementation will probably need a try/catch, but for brevity, we'll omit it
        return this.checked_add(Span.parse(other))
}

This same kind of principle could be applied in many methods.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions