F3XTeam / RBX-Try-Library

A library for controlling the flow of error-prone, interdependent functions.
12 stars 3 forks source link

Add concise way of calling object methods #5

Open GigsD4X opened 7 years ago

GigsD4X commented 7 years ago

Making calls to object methods currently requires both passing the function, and the receiving object, which usually means the receiving object is referenced twice in the call to Try. Code that calls object methods through Try would be clearer if this could be abstracted by the library somehow.

Validark commented 7 years ago

Can you give example code to illustrate what you're describing?

Validark commented 7 years ago

After reading that a bunch of times, I assumed you were talking about this:

Try(HttpService.GetAsync, HttpService, 'http://httpstat.us/503')

There isn't really a great way to change this, as methods are methods for a reason. The only other way to do it is like so:

Try(HttpService, "GetAsync", 'http://httpstat.us/503')
GigsD4X commented 7 years ago

We can probably change the behavior of Try based on the function signature to pull this off without breaking compatibility; this would also make implementing #4 simpler 🤔

Try([Boolean Async = false,] Callable Function, Varied Arguments...) → Attempt Try([Boolean Async = false,] <table, userdata> Object, String Method, Varied Arguments...) → Attempt

In the latter case, the metatable of Object would be checked to ensure that a __call metamethod isn't defined. This probably makes it difficult to use methods from callable objects however, so perhaps the following makes more sense:

Try([Boolean Async = false,] Callable Function, Varied Arguments...) → Attempt Try([Boolean Async = false,] Table Method, Varied Arguments...) → Attempt

Where table Method would be a table of the form {<table, userdata> Object, String MethodName}, such that you could do:

Try({ HttpService, 'GetAsync' }, 'http://httpstat.us/503')

The performance hit of using this syntactic sugar would come from creating the table plus the additional type and metatable checks when not using the simpler type signature, but it's likely neglible and worth the value in most cases (in cases that call for extreme optimization, the call can be written to not use the syntactic sugar, thereby avoiding it)

Validark commented 7 years ago

I'm not a fan of table generation in parameters, there's already more than enough of that in this library already. It could easily be scripted in such a way that it accepts both forms, but honestly you might consider just keeping it the way it is.

You might not like seeing HttpService in there twice, but that's the way pcall works too, and any changes to the way Try's parameters work would take away it's status as a pcall wrapper. People already know how to use pcall, and the syntax should probably stay true to pcall.

Validark commented 7 years ago

Because Then is already ran within a pcall, why not just do this?

Try(wait, 0.5)
    :Then(function()
        return HttpService:GetAsync("weburl")
    end)

    :Then(function(Data)
        return HttpService:JSONDecode(Data)
    end)