-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Proposal] Allow flag usage in native blocks #167
Comments
For comparison: /// Current:
#if d
native {
module hellofu;
}
#endif
public static class HelloFu {
/// Return hello message
public static string GetMessage() {
#if d
native {
import std.stdio;
writeln("This is a D injection!");
}
#elif js
native {
console.log("This is a JS injection!");
}
#endif
#if c
return "Hello C!";
#else
return "Hello World!";
#endif
}
}
---------------------------------------------------------
/// Proposed:
flag(D) native {
module hellofu;
}
public static class HelloFu {
/// Return hello message
public static string GetMessage() {
flag(D) native {
import std.stdio;
writeln("This is a D injection!");
}
flag(JS) native {
console.log("This is a JS injection");
}
flag(C) {
return "Hello C!"
} else {
return "Hello World!";
}
}
}
|
D's I don't expect I could consider adding an argument to |
Tbh I don't think most of C# programmers even know about
Most programmers right now are python, js/ts (or web in general), rust (which has attributes and macros as way for conditional compilation). And imo C/C++ programmers wouldn't use fusion, unless they really need (as of right now) JavaScript and TypeScript as from all other languages have at least some sort of C interop.
Isn't
That's fair, but it's a single project. There are many cases where one would want a bunch of native blocks: attributes (c++, c#, java, d...), unittests (d (idk if there are others with native unittest support)), lang-specific libraries, inline assembly (c, c++, d). For conditional compilation it can be just doing different thing in different languages. And as amount of languages grow there will be more and more edge cases where #ifs and native blocks would become nessesary
That is fair, but what would be syntax? There are basically two ways I see right now without changing much,
First falls under your unfamiliar syntax argument and second sticks out enough to prompt programmer to look at docs, so... I guess second?
Oh, on that note there's also a Also another thing why I made this proposal is readability. C-style preprocessor statements take a lot of space since they're mostly line-based (and I personally stand for OTBS). Here's an argument, which would be eliminated if to integrate (optional) flags into native blocks // currently
int func() {
#if FLAG
native {
// code
}
#endif
}
// Or
int func() {
#if FLAG
native {
// code
}
#endif
}
// Or
int func() {
#if FLAG
native {
// code
}
#endif
} // what I was proposing
int func() {
flag(FLAG) native {
// code
}
} // if to integrate flags into native
int func() {
native FLAG {
// code
}
} For me in current way all three options are bad way to write code. Both my proposed way and native flags way are more natural, so, if not have better If you'd rather go with flags in native, then please rename this issue, just so it's easier to track |
I don't know what's your experience with C#, but from 2004 to 2014 I worked full-time as a C# developer, on a team of a dozen developers.
Could you elaborate on your point of view? Fusion is an alternative to C interop and I expected the target audience to be people tired of developing libraries in C with tons of bindings.
I was referring to
Is your Fusion codebase open source? I have eight open source projects (fut being one of) and there's exactly one use of The expected integration with the target languages is to use Fusion-generated classes, sometimes subclassing them, not add much
First one looks good. I don't know if FLAG should be a preprocessor symbols expression or maybe a list of languages? (lower/uppercase?
This is again D syntax. As much as I have sympathy for D, I don't want to clone its syntax.
I find it an advantage: if conditional compilation looks bad, design your code to avoid it.
I'd be happy to see your usecases! Fusion wasn't designed in vain. From the very beginning, it served as an implementation language for working projects.
|
Ok, I see your points (with a bit clearer mind) and I'd say let's stick to thinking about improving So, potential solutions are: // i assume capital flags here
// because they are easier
// to differentiate from normal
// syntax and symbols
// more like a preprocessor expr
native C {}
// more functional approach
native(C) {}
// if to allow a list of languages
native(C || CPP || OPENCL) {}
// commas feel best for func style
native(C, CPP, OPENCL) {}
// and || feels best for preproc
native C || CPP {}
native C, CPP {} Either one is fine, though func feels cleaner. I'd say your choice which one it'd be. The question is, would language flags be predefined when compiling to that languahe or if it'd be up to user to define it with Another question is if it'd be predefined would it be allowed to use user-defined flags (I vote for yes). For example if languages are defined and I want to use some generic code for, let's say, both C, C++ and OpenCL and I'd define |
This looks best:
I think fut should predefine the preprocessor symbol for the language it is transpiling to, to make it standard and easy-to-use. Perhaps we should drop the condition-less As we're brainstorming here, in the beginning of Fusion, I also considered
The |
What if
Or And tbh I don't think there's going to be any collision with languages, there's barely any with same names. I know, I've looked at a lot of them. I'd say I saw more then 200 and only collisions I saw were some unknown toy languages. Also I'm personally not fan of Yea, maybe remove second bar and certainly allow having full on expressions! |
I didn't mean different languages having the same name. I meant that if someone uses The |
You could also add a warning/error when user flags are colliding with predefined ones so that it's more apparent |
Current way of doing conditional compilation is nice, but it is very reminiscent of C's
#ifdef
s, which when used withnative
blocks is kind of cumbersome.From all languages I've seen D seems to handle conditional compilation the best. For checking compiler flags (like fut's
-D
) it usesversion
blocks, which are used in this way:Which lands itself nicely with chaining code blocks
My proposal is to instead of current conditional compilation blocks introduce something similar to D's
version
statement.Example:
And since it's code block based it lands nicely with both native blocks and out of box elseif support.
More about conditional compilation in D: https://dlang.org/spec/version.html (there's a bunch more interesting features)
Related: #142
The text was updated successfully, but these errors were encountered: