How do you use an operation written in another file in Q#? - import

File A has
Operation Foo() : () {
body{
...
}
}
I want to use Foo in another operation in File B
Operation Bar() : (){
// How to use Foo?
}
File A and B may not be in the same folder.

There are two parts to making this work: namespaces and project references.
All operations (and pretty much everything else) in Q# are in some namespace. Check the namespace directives at the tops of the two files; if the namespace names are the same, you're done with this part. If not, then in file B add an open directive at the top of the namespace that references the namespace for file A:
namespace A {
open B;
If files A and B are in the same folder (same project, if using full Visual Studio), then that's all you need. If not, then you need to add a reference from project B to project A. In Visual Studio, you right-click on project B, choose Add, and then Reference..., click Project on the left of the dialog that comes up, and select project A. See https://learn.microsoft.com/en-us/visualstudio/ide/how-to-add-or-remove-references-by-using-the-reference-manager for more details.
If you're using Visual Studio Code, then use the dotnet add reference command to add a reference to project A from project B. See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-add-reference.

Related

How to set Eclipse CDT to index / resolve std files

I have the following piece of code:
#include <iostream>
using namespace std;
int main()
{
vector<int> v; //Symbol vector could not be resolved
return 0;
}
The IDE complains about "vector": Symbol vector could not be resolved.
If I right click the "vector" keyword -> Source -> Add Include, nothing happens.
If I manually add #include <vector>, then everything is just fine, the file is indexed and I can use its member functions.
However, I expect the IDE to generate these include files for me, instead of manually adding them. How to setup eclipse to work like this?
I am using Ubuntu 16.04 and Eclipse CDT Neon.
This will only work if another file in your project already includes <vector>.
The way Add Include works is it searches the project's index for the name it's invoked on. If it finds a binding (function, type, etc.) corresponding to that name in the index, it sees what file declares that binding, and then includes that file for you.
For this to work, the binding corresponding to the name must be in the index already. For bindings declared in files external to your project (such as standard library headers), that will only be the case if the external file is already included by some file in your project.
#HighCommander4 - I noticed, through practice, the indexer behaves like in your description. There must be another file which already includes vector in order to have it in the index.
Given this behavior we can do the following:
One workaround is to have a dummy cpp file including <bits/stdc++.h>. Most of the stl headers are there. The header is available for GCC. For MSVC we can simply copy the content of into this dummy cpp file.
Another workaround is to add a linked folder to the location of the stl, in my case this is /usr/include/c++/5. In this case the whole stl library gets indexed from the very beginning.

Reference function within package matlab

I would like to structure my project in several packages. Each package should be each own namespace (so as to avoid conflicting filename) but within a package, I want everything to be in the same namespace (without having to put all the files in the same folder; I'd like different folders).
In practice I would like this structure
Project
main.m
commonLibrary
+part1Project
mainPart1.m
otherFolder
supportFile.m
+part2Project
mainPart2.m
otherFolder2
supportFile2.m
This is the behavious I would like:
When in main.m, I can call everything in common library and everything in any sub-project, including the functions inside the subfolder. So I would like to call part1Project.supportFile
When in mainPart1.m, I want to call the support files without using the prefix of the current package (i.e. I want to call supportFile directly)
When in mainPart2, I want to call supportFile2 directly. If I want access to files in the the part 1 of the project, I can call part1Project.supportFile.
The current setup is that I added the Project folder and all the subfolders to the matlab path. But this means that
I CANNOT call supportFile from anywhere; not from main (part1Project.supportFile will not work) and not even from mainPart1 (supportFile can't be found)
Much in the same way, it is hard to access elements of part1Project from part2Project
How can I achieve the behaviour I want?
You cannot access functions within a subfolder of a package unless that subfolder is a private folder in which case it will only be accessible to the functions in the immediate parent folder.
If you do use the private folder approach, then you can call functions within this private folder from the functions in the containing folder without using the fully-qualified package name.
Your layout would look like:
Project
main.m
commonLibrary
+part1Project
mainPart1.m
private
supportFile.m
+part2Project
mainPart2.m
private
supportFile2.m
Your first point will not work but the other two will. There is no built-in way to accomplish the first point.
Another option would be to use import statements in all functions within each package such that it imports all package members at the beginning of the function.
Your layout would look like
Project
main.m
commonLibrary
+part1Project
mainPart1.m
supportFile.m
+part2Project
mainPart2.m
supportFile2.m
And the contents of mainPart1.m (any any function) would look something like:
function mainPart1()
% Import the entire namespace
import part1Project.*
% No package name required
supportFile()
end
And then from main you could access supportFile
function main()
part1Project.supportFile()
end

Source subdirectories in Swift package

In a library package, I would like to move some source files from the "Sources" folder to subdirectories, without changing language semantics (module name, visibility, etc).
Now I have a layout like:
LibraryProject
Sources
AnotherThing.swift
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
BarProtocol.swift
SomeBar.swift
OtherBar.swift
And, if I change it to something like:
LibraryProject
Sources
AnotherThing.swift
Foo
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
Bar
BarProtocol.swift
SomeBar.swift
OtherBar.swift
Then, invoking swift build fails:
error: the package has an unsupported layout, unexpected source file(s) found: [...]
Is this layout possible? I only found this issue https://bugs.swift.org/browse/SR-66 that suggests that it is not, but I cant find confirmation (or reason) in the documentation.
Thanks
I have found two options that work for Swift projects on Linux, either all .swift files must be directly in the Sources folder, or there must be one subfolder in Sources and as many subfolders within that as you like.
Swift builds a Module out of the top-level subfolder in Sources and includes all the subfolders within that.
I don't believe it is possible to have two Modules within the same Sources folder, as a Module would not recognise any code outside itself.
So in your example a working structure would be:
LibraryProject
Sources
YourModuleName
AnotherThing.swift
Foo
FooProtocol.swift
SomeFoo.swift
OtherFoo.swift
Bar
BarProtocol.swift
SomeBar.swift
OtherBar.swift
Here is the Folder structure for Swift package
And Here i have mentioned how to add the resources and add lines in Package

scala: relationship between package and source directory hierarchy

Does the package declared in the source file must match the directory hierarchy? For example, in source file a/b/c/foo.scala, the package must be a.b.c? If so, why bother to repeat again in the source file? If not, since the source file could declare any package it belongs to, then what's the usage of the directory hierarchy?
And is there some restriction about what could be inside the source file? For example, does the foo.scala could contain public class foo, and other classes or functions must be private or package access only?
Its not a requirement, but it the usual behaviour. A file a/b/c/foo.scala could be package x.y.z. I suppose you could say directories manage the way you store your code, and packages manage the way you expose your code.
There are no restrictions about what can be inside the source file (This is different to Java). foo.scala could contain the public class bar, or foo, or both, for example.

Reading file from classpath location for "current project"

i have 2 projects A and B in eclipse. B has dependency to A. In project A is a text file "file.txt". This file can be loaded in project B by for example getClass().getResourceAsStream(...)... because the location of the file is in classpath. I want to provide this same file name in project B and do some action only if file exists in project B. If there is no file in project B then no file should be found during loading with getClass().getResourceAsStream(...). Currently the file will be always found.
Thank you for help.
Kind regards
Sebastian
I need to provide a little bit more information about the problem. Sorry.
There is a project C which is the core. In project C there is class which do the following:
Check if file.txt exists. This is the base code which checks only the file name. No package name. And i dont want to change it.
if yes then do something.
if now then do something else.
A depends on C and B also depends on C. If A will be executed then the base code from C will be try to find file.txt.
If B will be executed then the base code will be also try to find file.txt. If there is no file in B project then it will found because it is in A. I dont want it.
Thank your very much.
Add the file in project B in a different classpath location, you try first the location in project B and then check the file in project A. Say the location is com.myPackage.file1 in project B and com.myPackage.projA.file1 in project A.