www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - public imports

reply Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
A small question.
Is it intended behaviour that public imports inside function 
calls fail with the message "Error: found public instead of 
statement", or is it an underdocumented feature?

void foo()
{
     public import bar;
}
Dec 05 2018
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 5 December 2018 at 21:13:29 UTC, Sjoerd Nijboer 
wrote:
 A small question.
 Is it intended behaviour that public imports inside function 
 calls fail with the message "Error: found public instead of 
 statement", or is it an underdocumented feature?
Looks intended. It doesn't really make sense to have a public import inside a function.
Dec 05 2018
parent reply Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
On Wednesday, 5 December 2018 at 21:21:12 UTC, Adam D. Ruppe 
wrote:
 Looks intended. It doesn't really make sense to have a public 
 import inside a function.
I was trying to find a weird corner of the language and maybe do something funny with conditional imports. They don't work in functions, however public imports do work in templates, but act as imports that are only visible in that scope. I was trying to get some form of persistant import outside of the function/template scope in the module scope, depending on the parameters of the function or template. I hoped I could find something funny or maybe some usefull trick. Unfortunately I couldn't :/
Dec 05 2018
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Wed, Dec 05, 2018 at 10:57:37PM +0000, Sjoerd Nijboer via
Digitalmars-d-learn wrote:
[...]
 I was trying to get some form of persistant import outside of the
 function/template scope in the module scope, depending on the
 parameters of the function or template.  I hoped I could find
 something funny or maybe some usefull trick.  Unfortunately I couldn't
 :/
[...] Maybe if you described to us exactly what you want to do, we could find a way to do it that doesn't involve language holes that are not guaranteed to work? T -- Spaghetti code may be tangly, but lasagna code is just cheesy.
Dec 05 2018
parent Sjoerd Nijboer <dlang sjoerdnijboer.com> writes:
On Wednesday, 5 December 2018 at 23:18:49 UTC, H. S. Teoh wrote:
 Maybe if you described to us exactly what you want to do, we 
 could find a way to do it that doesn't involve language holes 
 that are not guaranteed to work?
Honestly I don't know. I was just messing around. My initial question was, is this a bug or a feature. For which I got an answer.
Dec 05 2018
prev sibling parent Paul Backus <snarwin gmail.com> writes:
On Wednesday, 5 December 2018 at 21:13:29 UTC, Sjoerd Nijboer 
wrote:
 A small question.
 Is it intended behaviour that public imports inside function 
 calls fail with the message "Error: found public instead of 
 statement", or is it an underdocumented feature?

 void foo()
 {
     public import bar;
 }
An import statment inside a function is already limited to that function's scope, so making it public doesn't really make sense. That said, the language specification [1] doesn't say anything about this kind of import being an error, so it's definitely underdocumented. The error message is also not as good as it could be. [1] https://dlang.org/spec/module.html#scoped_imports
Dec 05 2018