0

I need to reproduce the functionality of this C code but in typescript. The purpose of this code is mainly to simplify error checking as stated in JPL's The Power of Ten but I couldn't find a way to do it in TS.


#define ecall(retVal, l_call, format, ...) do { \
        int _rv = (l_call);                     \
        if(_rv < 0) {                           \
            printf(format, __VA_ARGS__);        \
            return retVal;                      \
        }                                       \
        else {                                  \
            check();                            \
        }                                       \
    } while(0)

Pablo Ariel
  • 251
  • 3
  • 16
  • 4
    There is no such thing as macro expansion in Typescript (or Javascript for the matters). Just declare a normal function and call it whenever you need it. – Alejandro Jul 21 '22 at 21:46
  • I think you need to tag this with more than just TypeScript; do people need to understand C and C macros to answer this? Do people need to know what `check()` is? – jcalz Jul 22 '22 at 01:39
  • @Alejandro that's not an option unless you don't understand what the macro is for, plus it would multiply x6 the code size, so I need to know how to rewrite some sane C++ code in TypeScript without making it insane. This is really important because this code is almost the same as that suggested by The Power of Ten document by NASA/JLP which is mandatory for the kind of certainty and introspection we need in the code as it makes the difference between a project costing millions or not and between working properly or not. – Pablo Ariel Aug 08 '22 at 11:00
  • @jcalz they don't need to know what `check()` is. They need to understand what `return` does. I think @Alejandro wasn't able to understand that you can't move a return to a function while preserving its functionality. – Pablo Ariel Aug 08 '22 at 11:03
  • @KamilCuk Your rant proves nothing compared to the actual data we consider for our choices and even more because you have no experience enough on the topic as to participate in the discussion. If you have no idea about what you're talking about I'd say you better refrain from commenting and nothing of what you said proves anything other than that you have no idea what you're talking about. – Pablo Ariel Aug 22 '22 at 02:16
  • My intention was to ask questions for clarifications. Sure. – KamilCuk Aug 22 '22 at 04:44
  • 1
    Don't try to write typescript to look exactly like C. You can throw, or return an Either type, or whatever – Caleth Mar 16 '23 at 12:30

1 Answers1

1

maybe you can try ts-macro, as its name, impl macro for ts based with ttypescript.

D.H.Lolo
  • 53
  • 4