Skip to content

add docs for usingnamespace #1589

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

Closed
1 of 4 tasks
andrewrk opened this issue Sep 25, 2018 · 5 comments
Closed
1 of 4 tasks

add docs for usingnamespace #1589

andrewrk opened this issue Sep 25, 2018 · 5 comments
Labels
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Sep 25, 2018

usingnamespace @import("foo.zig");
  • might plan to rename the keyword?
  • might plan to get rid of it?
  • try not to use this feature until it becomes stable.
  • however it makes sense to use the feature to combine imports and re-export them, like this
@andrewrk andrewrk added the docs label Sep 25, 2018
@andrewrk andrewrk added this to the 0.4.0 milestone Sep 25, 2018
@ghost
Copy link

ghost commented Sep 26, 2018

When I look at Zig projects on the internet I have to say that I'm impressed by how little people use the 'use' keyword, I'm the exact opposite and I just can't seem to stop using it.

Is it better to prepend every SDL_* function with 'c.' or 'sdl.'? Does that make it clearer where it comes from? The way I have things setup, I can press 't' to instantly go to a symbol. And thanks to zig I can grep for "pub fn NameOfFunction" to find it instantly. Why do I need a path to each symbol spelled out for me? Everyone knows vk, gl, SDL symbols are going to be coming from a single file or a few files someplace. For me, those symbols are clear enough without an explicit path.

I take compilation time very seriously. If the lack of 'use' speeds of compilation time, then I may see the point. However, because of namespace per file, it wouldn't surprise me if people write more methods than they would have otherwise so it's easier to (sort of) escape the namespace mechanism:
const Struct = @import("file.zig").Struct;

No one's going to be pulling out individual functions (when there's a lot of them), so writing methods is easier. But the act of doing this reduces the compile time penalty from using 'use' and makes it much easier to guess where symbols come from. So it would seem that struct methods are already providing both the ability to reduce compile time and see where symbols come from, making the namespace per file unnecessary in my opinion. Why have a namespace for a file that contains just a single struct?

I think I'm in a very small minority here.

@tiehuis
Copy link
Member

tiehuis commented Sep 27, 2018

@UniqueID1 see also #1047.

@andrewrk
Copy link
Member Author

I'm impressed by how little people use the 'use' keyword

I'm guessing that's due to lack of docs and examples, which isn't entirely unintentional. (Although of course nearer to zig's completion it will not be a valid strategy)

Is it better to prepend every SDL_* function with 'c.' or 'sdl.'?

One thing I'll point out is that the common prefix on C functions is essentially a namespace, even though it's part of the symbol name. E.g. SDL_CreateTexture and SDL.CreateTexture are equivalent in meaning and in number of characters, but the second example uses an actual namespace.

One more thing, you can see for the LLVM functions that the self hosted compiler uses, I did this:

pub const ABIAlignmentOfType = c.LLVMABIAlignmentOfType;
pub const AddAttributeAtIndex = c.LLVMAddAttributeAtIndex;
pub const AddFunction = c.LLVMAddFunction;
pub const AddGlobal = c.LLVMAddGlobal;
pub const AddModuleCodeViewFlag = c.ZigLLVMAddModuleCodeViewFlag;
pub const AddModuleDebugInfoFlag = c.ZigLLVMAddModuleDebugInfoFlag;
pub const ArrayType = c.LLVMArrayType;
pub const BuildLoad = c.LLVMBuildLoad;
pub const ClearCurrentDebugLocation = c.ZigLLVMClearCurrentDebugLocation;
pub const ConstAllOnes = c.LLVMConstAllOnes;
pub const ConstArray = c.LLVMConstArray;
pub const ConstBitCast = c.LLVMConstBitCast;
pub const ConstInt = c.LLVMConstInt;
pub const ConstIntOfArbitraryPrecision = c.LLVMConstIntOfArbitraryPrecision;
pub const ConstNeg = c.LLVMConstNeg;
pub const ConstNull = c.LLVMConstNull;
pub const ConstStringInContext = c.LLVMConstStringInContext;
pub const ConstStructInContext = c.LLVMConstStructInContext;
pub const CopyStringRepOfTargetData = c.LLVMCopyStringRepOfTargetData;
pub const CreateBuilderInContext = c.LLVMCreateBuilderInContext;
pub const CreateCompileUnit = c.ZigLLVMCreateCompileUnit;
pub const CreateDIBuilder = c.ZigLLVMCreateDIBuilder;
pub const CreateEnumAttribute = c.LLVMCreateEnumAttribute;
pub const CreateFile = c.ZigLLVMCreateFile;
pub const CreateStringAttribute = c.LLVMCreateStringAttribute;
pub const CreateTargetDataLayout = c.LLVMCreateTargetDataLayout;
pub const CreateTargetMachine = c.LLVMCreateTargetMachine;
pub const DIBuilderFinalize = c.ZigLLVMDIBuilderFinalize;
pub const DisposeBuilder = c.LLVMDisposeBuilder;
pub const DisposeDIBuilder = c.ZigLLVMDisposeDIBuilder;
pub const DisposeMessage = c.LLVMDisposeMessage;
pub const DisposeModule = c.LLVMDisposeModule;
pub const DisposeTargetData = c.LLVMDisposeTargetData;
pub const DisposeTargetMachine = c.LLVMDisposeTargetMachine;
pub const DoubleTypeInContext = c.LLVMDoubleTypeInContext;
pub const DumpModule = c.LLVMDumpModule;
pub const FP128TypeInContext = c.LLVMFP128TypeInContext;
pub const FloatTypeInContext = c.LLVMFloatTypeInContext;
pub const GetEnumAttributeKindForName = c.LLVMGetEnumAttributeKindForName;
pub const GetHostCPUName = c.ZigLLVMGetHostCPUName;
pub const GetMDKindIDInContext = c.LLVMGetMDKindIDInContext;
pub const GetNativeFeatures = c.ZigLLVMGetNativeFeatures;
pub const GetUndef = c.LLVMGetUndef;
pub const HalfTypeInContext = c.LLVMHalfTypeInContext;
pub const InitializeAllAsmParsers = c.LLVMInitializeAllAsmParsers;
pub const InitializeAllAsmPrinters = c.LLVMInitializeAllAsmPrinters;
pub const InitializeAllTargetInfos = c.LLVMInitializeAllTargetInfos;
pub const InitializeAllTargetMCs = c.LLVMInitializeAllTargetMCs;
pub const InitializeAllTargets = c.LLVMInitializeAllTargets;
pub const InsertBasicBlockInContext = c.LLVMInsertBasicBlockInContext;
pub const Int128TypeInContext = c.LLVMInt128TypeInContext;
pub const Int16TypeInContext = c.LLVMInt16TypeInContext;
pub const Int1TypeInContext = c.LLVMInt1TypeInContext;
pub const Int32TypeInContext = c.LLVMInt32TypeInContext;
pub const Int64TypeInContext = c.LLVMInt64TypeInContext;
pub const Int8TypeInContext = c.LLVMInt8TypeInContext;
pub const IntPtrTypeForASInContext = c.LLVMIntPtrTypeForASInContext;
pub const IntPtrTypeInContext = c.LLVMIntPtrTypeInContext;
pub const IntTypeInContext = c.LLVMIntTypeInContext;
pub const LabelTypeInContext = c.LLVMLabelTypeInContext;
pub const MDNodeInContext = c.LLVMMDNodeInContext;
pub const MDStringInContext = c.LLVMMDStringInContext;
pub const MetadataTypeInContext = c.LLVMMetadataTypeInContext;
pub const ModuleCreateWithNameInContext = c.LLVMModuleCreateWithNameInContext;
pub const PPCFP128TypeInContext = c.LLVMPPCFP128TypeInContext;
pub const PointerType = c.LLVMPointerType;
pub const SetAlignment = c.LLVMSetAlignment;
pub const SetDataLayout = c.LLVMSetDataLayout;
pub const SetGlobalConstant = c.LLVMSetGlobalConstant;
pub const SetInitializer = c.LLVMSetInitializer;
pub const SetLinkage = c.LLVMSetLinkage;
pub const SetTarget = c.LLVMSetTarget;
pub const SetUnnamedAddr = c.LLVMSetUnnamedAddr;
pub const SetVolatile = c.LLVMSetVolatile;
pub const StructTypeInContext = c.LLVMStructTypeInContext;
pub const TokenTypeInContext = c.LLVMTokenTypeInContext;
pub const VoidTypeInContext = c.LLVMVoidTypeInContext;
pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;
pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext;

I can't remember if I opened a proposal for doing something like this automatically or not, but there should at least be a proposal.

I take compilation time very seriously.

Me too. That's the reason for opening #1535, and you can see in the self-hosted compiler I'm going to great lengths to fully utilize all the cores of a machine, cache as much as possible (in memory!), and take advantage of language semantics in order to divide up compilation into independent work units.

I think as that work comes to fruition we can re-evaluate use and the evolution of the feature.

@BarabasGitHub
Copy link
Contributor

BarabasGitHub commented Sep 27, 2018 via email

@ghost
Copy link

ghost commented May 31, 2019

This issue should be renamed to "add docs for usingnamespace"

@andrewrk andrewrk changed the title add docs for use add docs for usingnamespace Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants