useSWR key type control - swr

I want to use useSWR hook but I would like to provide more type safety, because now it allows to pass anything as key which is really error prone. Is there any good way to provide safety without writing own wrapper on useSWR?

Related

PLT redex: Write custom test assertions

I’d like to define a test assertions myself, i.e. not just use the predefined (test-->> …) etc combinators, and I wonder how to register the test result (success, or failure with a string) with the test driver.
Looking at their code I see that (inc-failures), (inc-successes) and (eprintf), but if I try to use them myself, I get
inc-successes: unbound identifier
How can I get hold of this? Or is this not possible, because it’s defined in private/reduction-semantics.rkt, but not provided from that file? If it it is not possible, what is the best practice for implementing new test combinators?

Is it a good practice to use Nothing in generics?

Like in this example:
sealed class Option<T>
object None : Option<Nothing>() // <-- like this
class Some<T> : Option<T>()
Or, if it's not a good practice, what should I use here instead?
Are there any official response/article on that? Or is there any argumentation that this is a good practice?
I know that Nothing was designed to be used as a type for return value for functions that never returns any value, so I'm not sure if using it as a generic parameter is a valid use.
I know there is an article that says that you can do that, but I'm not sure if I can trust it.
And the author of koptional uses it too, but I don't know if I can trust that either.
Also, it looks like in Scala Option is implemented similar to that, None have type Option[Nothing] and Scala's Nothing is similar to Kotlin's Nothing.
I agree with #zsmb13's comment. Using Nothing in a generic type hierarchy is perfectly valid and even gives benefits over other options:
First, Nothing is embedded in the Kotlin type system as a subtype of any other type, so it plays well with generics variance. For example, Option<Nothing> can be passed where Option<out Foo> is expected.
Second, the compiler will perform control flow checks and detect unreachable code after a Nothing-returning member call when the type is statically known.
See also: A Whirlwind Tour of the Kotlin Type Hierarchy

Is there an efficient way to avoid instantiating a class with syntax errors?

As you may know, it is pretty easy to have active code of a class containing syntax errors (someone activated the code ignoring syntax warnings or someone changed the signature of a method the class calls, for instance).
This means that also dynamic instantiation of such a class via
CREATE OBJECT my_object TYPE (class_name).
will fail with an apparently uncatchable SYNTAX_ERROR exception. The goal is to write code that does not terminate when this occurs.
Known solutions:
Wrap the CREATE OBJECT statement inside an RFC function module, call the module with destination NONE, then catch the (classic) exception SYSTEM_FAILURE from the RFC call. If the RFC succeeds, actually create the object (you can't pass the created object out of the RFC because RFC function modules can't pass references, and objects cannot be passed other than by reference as far as I know).
This solution is not only inelegant, but impacts performance rather harshly since an entirely new LUW is spawned by the RFC call. Additionally, you're not actually preventing the SYNTAX_ERROR dump, just letting it dump in a thread you don't care about. It will still, annoyingly, show up in ST22.
Before attempting to instantiate the class, call
cl_abap_typedescr=>describe_by_name( class_name )
and catch the class-based exception CX_SY_RTTI_SYNTAX_ERROR it throws when the code it attempts to describe has syntax errors.
This performs much better than the RFC variant, but still seems to add unnecessary overhead - usually, I don't want the type information that describe_by_name returns, I'm solely calling it to get a catchable exception, and when it succeeds, its result is thrown away.
Is there a way to prevent the SYNTAX_ERROR dump without adding such overhead?
Most efficient way we could come up with:
METHODS has_correct_syntax
IMPORTING
class_name TYPE seoclsname
RETURNING
VALUE(result) TYPE abap_bool.
METHOD has_correct_syntax.
DATA(include_name) = cl_oo_classname_service=>get_cs_name( class_name ).
READ REPORT include_name INTO DATA(source_code).
SYNTAX-CHECK FOR source_code MESSAGE DATA(message) LINE DATA(line) WORD DATA(word).
result = xsdbool( sy-subrc = 0 ).
ENDMETHOD.
Still a lot of overhead for loading the program and syntax-checking it. However, at least none additional for compiling descriptors you are not interested in.
We investigated when we produced a dependency manager that wires classes together upon startup and should exclude syntactically wrong candidates.
CS includes don't always exist, so get_cs_name might come back empty. Seems to depend on the NetWeaver version and the editor the developer used.
If you are certain that the syntax errors are caused by the classes’ own code, you might want to consider buffering the results of the syntax checks and only revalidate when the class changed after it was last checked. This does not work if you expect syntax errors to be caused by something outside those classes.

Should I put assertions inside object's methods or writing and calling a check representation fuction is enough?t

I wonder if writing a check representation function for an object and calling it while testing is enough to check the object's internal state corectness or should I also put assertions inside methods? Or maybe I should only use assertions inside methods and just don't include them in production version to not slow my app down?

How can i invoke Vala methods dynamically?

I need to generate function name and then call it.
Is it possible to do like in php
<?php call_user_func_array(array($object, $method));?>?
There are four options:
Make the methods you want to call like this signals. Signals can be emited by name GLib.Signal.emit_by_name (g_signal_emit_by_name). The call is from GLib mode, but other modes with signal support are likely to have similar method.
Create a static table/hash table of delegate objects manually in code. The main advantage is that it is type-safe. Disadvantage is that you have to add each method in two places. It will also work in all vala modes.
Another option is to tell vala compiler to build the "gir" binding and use the GObject Introspection library to call the functions. That is much more complicated, but the compiler will maintain the list of available methods for you. This method is specific to the GLib mode.
The last option is to use the GLib.Module.symbol (g_module_symbol) function of GLib to find the symbol. You'll need to know the "mangled" C name of the symbol and it will not be type-safe. You will have to match argument types exactly and mind where the invocant should go. It avoids the overhead of GIR, but unlike GIR it can't tell you which methods exist, only get you a specific one. This method is used when connecting signals in GtkBuilder. I mentioned the function from GLib, but POSIX.dlsym can be used the same way.