Replies: 9 comments 8 replies
-
because of C++ naming, easy would be to create C wrapper extern "C" console_log(const char* msg)
{
std::cout << msg << "\n";
} then in TypeScript declare function console_log(msg: string);
class console
{
static const log = (msg:string) => console_log(msg);
}
function main1()
{
console.log("Hello World");
} |
Beta Was this translation helpful? Give feedback.
-
I've also looked and turned out it is possible to use c++ classes in C. Example: #include <iostream>
extern "C" {
typedef struct Console Console;
// Factory function
Console* console_constructor(std::ostream* outStream, std::ostream* errStream, bool ignoreErrors);
} Console.cpp: #include "Console.hpp"
struct Console {
std::ostream* outStream;
std::ostream* errStream;
bool ignoreErrors;
// ... log functions
};
Console* console_constructor(std::ostream* outStream, std::ostream* errStream, bool ignoreErrors) {
Console* console = new Console;
console->outStream = outStream;
console->errStream = errStream;
console->ignoreErrors = ignoreErrors;
return console;
} The final C code: #include "Console.hpp"
Console* console = console_constructor(&std::cout, &std::cerr, false); So we might not need to include something additional into ts code. |
Beta Was this translation helpful? Give feedback.
-
I have made a pattern of console object. But the issue is that it is hard to declare console in ts if build it following documentation (with different streams etc.). If try to wholy build the console in c++ code then the issue is when i need to create an object, where colorMode key must be either boolean or string. So it would require the tsc feature. Maybe create such object in ts and use in c++ code like library... If create the class in ts using just library, then i don't know how to make stream there (cout, cerr). Firstly i did feature wholy on c++ but when was need to use non-staric types i did like in your example. |
Beta Was this translation helpful? Give feedback.
-
This is attempt to create at c++ But the issue with dynamic object. To wholly implement console as in regular js must be access to tsc features. So for now i can create a library for structure only which will not support constructor and custom output. Since the custom console features are almost never used this would be enough for most ts code. (doing). |
Beta Was this translation helpful? Give feedback.
-
Please, check if i'm doing the console correctly |
Beta Was this translation helpful? Give feedback.
-
yes, good to know, I agree I don't think it would be effortless to compile console.js in typescript. it will require some adjustments for sure |
Beta Was this translation helpful? Give feedback.
-
not it is not early but I think we need to implement our code for JavaScript libs and I don't think we can just copy/paste code from "node" |
Beta Was this translation helpful? Give feedback.
-
Keep in mind it does also require 'Infinity" and '-Infinity' types |
Beta Was this translation helpful? Give feedback.
-
can be used the following |
Beta Was this translation helpful? Give feedback.
-
Would be good if the compiler support the default ts syntax with console class etc... And the advanced syntax on user requirements. For example implement a regular console class and do not require main function.
Console class:
Now the user can use console features and not 'print'. This means that the ts checker will not argue with code and you can compiler code that was not made for tsc.exe
I don't know whether you can add c++ code. But i can write more if you need.
Since the main function is simply the whole ts file, compiler can simply raise top functions and classes and automatically surround remaining code into main function. For example (as c++ but could be another language for your compiler):
result:
Beta Was this translation helpful? Give feedback.
All reactions