I have two question:
What is the use of file(remember file not class) in kotlin since we already have classes?
Can we communicate between two kotlin files. If yes then how?
Assuming you mean files ending in .kt that aren't classes, kotlin allows you to have variables and functions in the 'Global Scope' too in addition to classes. Files can access non private variables, functions, classes, etc. in other files in the same package without requiring any additional steps, but will need import statements to access files in different packages.
someText defined in File0.kt
const val someText = "asdf"
printSomeText function defined in File1.kt
fun printSomeText(){
print(someText)
}
Related
I've been struggling with this now longer than I should and surprisingly, the existing questions (and there are many) don't really help further.
My goal is to declare and define enum on a global level, such that I can use that said enum in any file without having to import them.
Now, to my understand you can't do this with a d.ts file. I also saw other work arounds (e.g. this one https://github.com/Microsoft/TypeScript/issues/14975 ) that don't seem best-practice but rather a "fix". Or questions, that aren't fully answered (e.g. Enums in typescript d.ts file )
Now, apparently a way to go is to create a common.ts and handle any enum in that file. How exactly do I have to proceed in order to make this possible:
Some global.ts file
enum someEnum{
first = 'someString',
second = "someOtherString'
}
a.ts file
//without any imports
const myFirstEnumValue = someEnum.first // resulting in "someString"
Context
I'm writing a React, Electron App and don't want to declare/define my enums twice, in electron.ts (sometimes called main) and in any react file. This works without any issue with types and interfaces in a file called types.d.ts (filepath is added to typeRoots in tsconfig.json) but again, not with enums
I am working on a project in C Visual Studio, and I have two sets of functions, let’s call them SET_1 and SET_2.
I wonder if there is a way to ensure that a function from SET_1 calls only functions from SET_1 and not functions from SET_2.
The simplest solution will be to split the project in 2, but I want to avoid any major refactoring. I probably can make some runtime checks but I want to avoid this too…
So, I am wondering if there is something like SAL annotations that I can use to enforce this isolation at compile time?
Here is an example of what I want:
#define SET_1 ...
#define SET_2 ...
SET_1
void Fct1()
{
// ...
}
SET_1
void Fct2()
{
Fct1(); // Ok, both functions have SET_1 tag
}
SET_2
void Fct3()
{
Fct1(); // Compile error, Fct1 has a different tag
}
I don’t want to write some kind of code parser to manually enforce this rule.
I have multiple files and a file contains functions from both sets. The functions don’t have any common characteristic, I manually need to specify the set for each function.
The solution can be at compile time or at build time. I just want to make sure that a function from set1 will not be called from set2
I can modify the code and I know that the right solution will be to refactor the project, but I am curious if there is another solution.
For example, if the code was in C++, I could include all functions from set1 inside a namespace and those from set2 inside another namespace. But this will not work if we have a class with function members in different sets.
I am writing a compiler for a toy OO language. I am writing it in C, using Flex and Bison.
Consider the following syntax:
class MyClass {
int m_n;
void MyFunc(int b) {
m_n = 5;
m_p = b;
}
int m_p;
}
My current code will complain that in MyFunc, m_p has not yet been declared (with good reason). So, I came to the conclusion that I need a multi-pass parsing technique - something along the lines of:
1st pass - process variable declarations
2nd pass - process function definitions
First - is this the best way to solve the issue? Are there other methods that I should look into? Second - if this is a favorable solution, would I go about implementing it with a re-entrant lexer/parser?
Thanks
I recently wrote a compiler for an OO language, we had multiple passes (depending on the complexity of the language of course):
Collect all Classes
Build up superclass hierarchy
Collect all methods and fields
Collect variables inside methods etc.
There are reasons why we had to split up the whole process into 4 passes:
You can't build up the superclass hierarchy when not all classes have been processed yet (led to 2. pass)
You can't validate inherited methods (return value, parameters etc.) when the superclass is unknown (led to 2. pass)
You can't process variables when not all fields have been collected yet (led to 4. pass)
You can leave out the second pass if you don't have inheritance in your language of course.
When I look at it now, it should've been possible to merge pass 2 and 3 as all data should be available for pass 3.
The way we implemented it was just by walking through the AST and annotating it with the required symbol tables.
What is the importance of Static keyword in Java and in C++ and how it's functionality differ in both programming languages ?
Maybe this link will give you a better idea: http://www.pp.rhul.ac.uk/~george/PH2150/html/node48.html
It has a visual diagram that may make it easier to understand.
There are 2 meanings for static. The first if you have a static variable, this means there is only 1 instance of this variable. It works pretty much the same in all programming languages with the keyword.
A static function is a function that can be called, even if the class it resides in is not instantiated. Static functions are necessary in C# and Java because you cant declare functions in these languages which have no encompassing class.
in C++, you can declare functions in the global namespace. In this language, static functions are used to denote that a function belongs to the class, but you dont have to instantiate the class to use the function. You could use a static function to access private variables of the class. Also note that in C++, static functions have a known memory address, so you can use function pointers to point to them without instantiating the class.
For Java, Understanding Instance and Class Members is a good place to start.
For C++, Microsoft has a reference on the static keyword.
There are many readily available programming language resources that will help you understand what the static keyword means. The above are two of them that I found with a quick Google search.
Use static for fields and methods that can only have one instance. That means they are not relevant to instances of a class, but to the class itself. For example the main thread (public static void main).
It works the same way in both languages. I assume you know what's object-oriented programming, and what's the difference between classes and objects/instances. So, if you mark a method or variable as "static", it operates on a class level, not instance level. All objects/instances share the same value of the "static" variable.
How can I create an array of namespaces? And because it seems like a long shot, if this is impossible, is there something similar to a namespace that can be made into an array?
The namespace, if it helps, contains these variables:
const int maxx=// depends on the particular namespace
// I need an array to go through each namespace and
// pick out the variable
const int maxy=// depends on particular namespace
//prgm is a class I made
prgm sector[maxx][maxy];
// another array of prgms. int is my shorthand of saying "depends on
// particular namespace", so is char.
prgm programs[int]={prgm1(int,int,char),prgm2(int,int,char)...
So any help would be welcome.
You could use reflection, but I think you should rethink your design.
I am not sure what language you are talking about, but in many (most?) languages, references to constants are replaced by the constant value at compile time. So they are no longer present at runtime and even reflection won't help.
You could create a class in each namespace that exposes the constants as (static) properties. Then you can use reflection to search the class in each namespace and obtain the constant values from the properties.
But, as mentioned by others, you should really rethink your design. Finally, namespaces are usually not accessable via reflection because they just extend the class names of the contained classes (and other stuff). Or is there a (non-esoteric) language that exposes namespaces as entities via reflection?
For .NET the reference for the System.Type.Namespace property states the following.
A namespace is a logical design-time naming convenience, used mainly to define scope in an application and organize classes and other types in a single hierarchical structure. From the viewpoint of the runtime, there are no namespaces.
Is this supposed to be C++? Sounds like you need to define a class, not a namespace, then create instances (objects) of that class and put them in an array.
So the sector variable gets tricky, since it is sized based on the value of maxx and maxy parameters that would be passed to the constructor of the class. You can take care of that problem by using a container class or a dynamically-allocated multi-dimensional array instead.
If you talk about C++, in there you can't pass namespaces as entities around. But you can do so with types, as type argument to templates. In this case, an MPL sequence could help together with MPL algorithms:
struct c1 { typedef int_<2> value_x; };
struct c2 { typedef int_<3> value_x; };
struct c3 { typedef int_<1> value_x; };
template<typename C> struct get_x : C::value_x { };
typedef vector<c1, c2, c3> scope_vec;
typedef max_element<
transform_view< scope_vec , get_x<_1> >
>::type iter;
You may then create your array like
prgm programs[deref< iter >::type::value];
Note that the search within that type-vector happens at compile time. So the value of the array is determined at compile time either.