www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - extending 'import' using 'with'

reply "Mike James" <foo bar.com> writes:
Just a thought...

How about adding the keyword 'with' to 'import' to save on typing 
:-)

import  org.eclipse.swt.widgets.Canvas,
         org.eclipse.swt.widgets.Composite,
         org.eclipse.swt.events.DisposeListener,
         org.eclipse.swt.events.DisposeEvent,
         org.eclipse.swt.events.PaintListener,
         org.eclipse.swt.events.PaintEvent;

import with (org.eclipse.swt) {
         widgets.Canvas,
         widgets.Composite,
         events.DisposeListener,
         events.DisposeEvent,
         events.PaintListener,
         events.PaintEvent;
}


Regards,

-=mike=-
Apr 01 2015
next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
On Wednesday, 1 April 2015 at 10:21:46 UTC, Mike James wrote:
 Just a thought...

 How about adding the keyword 'with' to 'import' to save on 
 typing :-)

 import  org.eclipse.swt.widgets.Canvas,
         org.eclipse.swt.widgets.Composite,
         org.eclipse.swt.events.DisposeListener,
         org.eclipse.swt.events.DisposeEvent,
         org.eclipse.swt.events.PaintListener,
         org.eclipse.swt.events.PaintEvent;

 import with (org.eclipse.swt) {
         widgets.Canvas,
         widgets.Composite,
         events.DisposeListener,
         events.DisposeEvent,
         events.PaintListener,
         events.PaintEvent;
 }


 Regards,

 -=mike=-
string importSubmodules(string rootModuleName, string[] subModuleNames) { import std.algorithm; import std.array; string prefix = "import " ~ rootModuleName ~ '.'; return subModuleNames.map!(name => prefix ~ name ~ ';').join } mixin(importSubmodules("org.eclipse.swt", [ "widgets.Canvas", "widgets.Composite", "events.DisposeListener", "events.DisposeEvent", "events.PaintListener", "events.PaintEvent" ])); Of course, why be clever here at all and do such things? It's an editor problem. Write the full import lines out, and if you hate typing out the path each time, use tricks in your editor to make that easier, or use an IDE.
Apr 01 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
people with Java/C/C++/etc. background tend to forget about the power of=20
metaprogramming: they have no such tool at hand, so they don't even think=20
about it.

three things that one need to become used of are UFCS (so std.algorighm=20
functions chains nicely), lambdas, and metaprogramming. but that has=20
downside: when you get used to those, you simply can't get back to Java/C/
C++/etc. anymore, they feels like something from the past age... ;-)=
Apr 01 2015
next sibling parent reply "w0rp" <devw0rp gmail.com> writes:
On Wednesday, 1 April 2015 at 13:35:22 UTC, ketmar wrote:
 people with Java/C/C++/etc. background tend to forget about the 
 power of
 metaprogramming: they have no such tool at hand, so they don't 
 even think
 about it.

 three things that one need to become used of are UFCS (so 
 std.algorighm
 functions chains nicely), lambdas, and metaprogramming. but 
 that has
 downside: when you get used to those, you simply can't get back 
 to Java/C/
 C++/etc. anymore, they feels like something from the past 
 age... ;-)
I tend to think mixins are used too much myself. They slow down compilation and inflate memory usage a lot. I always weigh the costs against the benefits.
Apr 01 2015
next sibling parent "John Colvin" <john.loughran.colvin gmail.com> writes:
On Wednesday, 1 April 2015 at 15:23:59 UTC, w0rp wrote:
 I tend to think mixins are used too much myself. They slow down 
 compilation and inflate memory usage a lot. I always weigh the 
 costs against the benefits.
While true currently, there's a lot of opportunity to improve the situation.
Apr 01 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 01 Apr 2015 15:23:58 +0000, w0rp wrote:

 I tend to think mixins are used too much myself. They slow down
 compilation and inflate memory usage a lot. I always weigh the costs
 against the benefits.
but they are sooooo handy! i... can't... stop... need... more... mixins...=
Apr 01 2015
prev sibling next sibling parent reply "Paulo Pinto" <pjmlp progtools.org> writes:
On Wednesday, 1 April 2015 at 13:35:22 UTC, ketmar wrote:
 people with Java/C/C++/etc. background tend to forget about the 
 power of
 metaprogramming: they have no such tool at hand, so they don't 
 even think
 about it.

 three things that one need to become used of are UFCS (so 
 std.algorighm
 functions chains nicely), lambdas, and metaprogramming. but 
 that has
 downside: when you get used to those, you simply can't get back 
 to Java/C/
 C++/etc. anymore, they feels like something from the past 
 age... ;-)
Actually metaprogramming is how a lot of magic happens in Java and .NET. It just tends to be frowned up in many companies due to maintenance headhaches when one gets too clever.
Apr 01 2015
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 01 Apr 2015 16:16:58 +0000, Paulo Pinto wrote:

 Actually metaprogramming is how a lot of magic happens in Java and .NET.
without on-the-fly code generation that's a mockery.=
Apr 01 2015
next sibling parent reply "Paulo Pinto" <pjmlp progtools.org> writes:
On Wednesday, 1 April 2015 at 19:04:43 UTC, ketmar wrote:
 On Wed, 01 Apr 2015 16:16:58 +0000, Paulo Pinto wrote:

 Actually metaprogramming is how a lot of magic happens in Java 
 and .NET.
without on-the-fly code generation that's a mockery.
You can generate code on the fly as well, that is a common trick to create, for example, optimized ORMs, regular expression engines or image processing algorithms that are then compiled to native code with the JIT. Runtime reflection, attribute processing, AST manipulation and bytecode generation can help a lot in meta-programming. It isn't as easy as D's mixins, but it gets the job done.
Apr 01 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
On Wed, 01 Apr 2015 19:36:53 +0000, Paulo Pinto wrote:

 On Wednesday, 1 April 2015 at 19:04:43 UTC, ketmar wrote:
 On Wed, 01 Apr 2015 16:16:58 +0000, Paulo Pinto wrote:

 Actually metaprogramming is how a lot of magic happens in Java and
 .NET.
without on-the-fly code generation that's a mockery.
=20 You can generate code on the fly as well, that is a common trick to create, for example, optimized ORMs, regular expression engines or image processing algorithms that are then compiled to native code with the JIT. =20 Runtime reflection, attribute processing, AST manipulation and bytecode generation can help a lot in meta-programming. =20 It isn't as easy as D's mixins, but it gets the job done.
as well as libjit and sparse for C, for example. but i will hardly name=20 that useful, let alone usable.=
Apr 01 2015
prev sibling next sibling parent Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 2015-04-01 at 19:04 +0000, ketmar via Digitalmars-d wrote:
 On Wed, 01 Apr 2015 16:16:58 +0000, Paulo Pinto wrote:
=20
 Actually metaprogramming is how a lot of magic happens in Java and=20
 .NET.
=20 without on-the-fly code generation that's a mockery.
You must not have done much JVM-based stuff recently, it's all about=20 "on the fly" code generation. Not to mention all the "as we load the=20 code" code generation, aka AOT. Java 9 and Java 10 are going to be=20 quite fun. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 01 2015
prev sibling parent "rumbu" <rumbu rumbu.ro> writes:
On Wednesday, 1 April 2015 at 19:04:43 UTC, ketmar wrote:
 On Wed, 01 Apr 2015 16:16:58 +0000, Paulo Pinto wrote:

 Actually metaprogramming is how a lot of magic happens in Java 
 and .NET.
without on-the-fly code generation that's a mockery.
Metaprogramming in .net is done through T4 templates. This is a Visual Studio feature, not a language feature. There are two types of T4 templates: - text templates - similar to mixins in D, you create a .tt file and each time you save it, a counterpart source file (even a .d source file) is created; - runtime text templates - code is generated and compiled at runtime on the fly. That's how most of the Visual Studio code designers and generators work. debugging.
Apr 02 2015
prev sibling next sibling parent Russel Winder via Digitalmars-d <digitalmars-d puremagic.com> writes:
On Wed, 2015-04-01 at 13:35 +0000, ketmar via Digitalmars-d wrote:
 people with Java/C/C++/etc. background tend to forget about the=20
 power of=20
 metaprogramming: they have no such tool at hand, so they don't even=20
 think=20
 about it.
Java metaprogramming is a real pain, so just use Groovy. (Though Java 8 at least has higher-order functions. method references and lambda=20 expressions.) C is a lost cause. C++ has a lot of template metaprogramming, but you have to be as good=20 as David Abrahams to be any good at it. Python (and Ruby, Lisp, Clojure, etc.) programmers have no problem=20 with this stuff. =20
 [=E2=80=A6]
--=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Apr 01 2015
prev sibling parent reply "Dennis Ritchie" <dennis.ritchie mail.ru> writes:
On Wednesday, 1 April 2015 at 13:35:22 UTC, ketmar wrote:
 people with Java/C/C++/etc. background tend to forget about the 
 power of
 metaprogramming: they have no such tool at hand, so they don't 
 even think
 about it.
C++ with boost: ----- #include <iostream> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/seq/for_each.hpp> namespace a { namespace b { namespace x { int test1 = 1; } namespace y { int test2 = 2; } namespace z { int test3 = 3; } } } #define USE_NAMESPACES_IMPL( r, root, name ) \ using namespace root :: name; #define USE_NAMESPACES( root, names ) \ BOOST_PP_SEQ_FOR_EACH( USE_NAMESPACES_IMPL, root, names ) USE_NAMESPACES( a::b, (x)(y)(z) ) int main() { std::cout << test1 << '\n'; // 1 std::cout << test2 << '\n'; // 2 std::cout << test3 << '\n'; // 3 return 0; } ----- http://ideone.com/LncucP
Apr 02 2015
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 4/2/15 2:52 AM, Dennis Ritchie wrote:
 On Wednesday, 1 April 2015 at 13:35:22 UTC, ketmar wrote:
 people with Java/C/C++/etc. background tend to forget about the power of
 metaprogramming: they have no such tool at hand, so they don't even think
 about it.
C++ with boost: ----- #include <iostream> #include <boost/preprocessor/cat.hpp> #include <boost/preprocessor/seq/for_each.hpp> namespace a { namespace b { namespace x { int test1 = 1; } namespace y { int test2 = 2; } namespace z { int test3 = 3; } } } #define USE_NAMESPACES_IMPL( r, root, name ) \ using namespace root :: name; #define USE_NAMESPACES( root, names ) \ BOOST_PP_SEQ_FOR_EACH( USE_NAMESPACES_IMPL, root, names ) USE_NAMESPACES( a::b, (x)(y)(z) ) int main() { std::cout << test1 << '\n'; // 1 std::cout << test2 << '\n'; // 2 std::cout << test3 << '\n'; // 3 return 0; } ----- http://ideone.com/LncucP
Boost's preprocessor library is a scar on the landscape of C++. It has wrecked such disasters on compilation times at work, that we're considering disallowing it with a lint rule. Andrei
Apr 02 2015
prev sibling next sibling parent "Jesse Phillips" <Jessekphillips+D gmail.com> writes:
On Wednesday, 1 April 2015 at 12:09:25 UTC, w0rp wrote:
 Of course, why be clever here at all and do such things? It's 
 an editor problem. Write the full import lines out, and if you 
 hate typing out the path each time, use tricks in your editor 
 to make that easier, or use an IDE.
I think this falls into the same camp as nested imports. Why doesn't it already work? Seems like an arbitrary limitation to me.
Apr 02 2015
prev sibling parent reply "weaselcat" <weaselcat gmail.com> writes:
On Wednesday, 1 April 2015 at 12:09:25 UTC, w0rp wrote:
 Of course, why be clever here at all and do such things? It's 
 an editor problem. Write the full import lines out, and if you 
 hate typing out the path each time, use tricks in your editor 
 to make that easier, or use an IDE.
no this is the reason java is unusable without an IDE.
Apr 02 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-04-02 21:21, weaselcat wrote:

 no
 this is the reason java is unusable without an IDE.
Yeah, in a Java IDE it would automatically add the missing imports. -- /Jacob Carlborg
Apr 03 2015
prev sibling next sibling parent "Brad Anderson" <eco gnuk.net> writes:
On Wednesday, 1 April 2015 at 10:21:46 UTC, Mike James wrote:
 Just a thought...

 How about adding the keyword 'with' to 'import' to save on 
 typing :-)

 import  org.eclipse.swt.widgets.Canvas,
         org.eclipse.swt.widgets.Composite,
         org.eclipse.swt.events.DisposeListener,
         org.eclipse.swt.events.DisposeEvent,
         org.eclipse.swt.events.PaintListener,
         org.eclipse.swt.events.PaintEvent;

 import with (org.eclipse.swt) {
         widgets.Canvas,
         widgets.Composite,
         events.DisposeListener,
         events.DisposeEvent,
         events.PaintListener,
         events.PaintEvent;
 }


 Regards,

 -=mike=-
Weird timing. I just had this idea a couple nights ago and was going to post about it here at some point. So I guess that means a +1 from me :).
Apr 01 2015
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2015-04-01 12:21, Mike James wrote:
 Just a thought...

 How about adding the keyword 'with' to 'import' to save on typing :-)

 import  org.eclipse.swt.widgets.Canvas,
          org.eclipse.swt.widgets.Composite,
          org.eclipse.swt.events.DisposeListener,
          org.eclipse.swt.events.DisposeEvent,
          org.eclipse.swt.events.PaintListener,
          org.eclipse.swt.events.PaintEvent;

 import with (org.eclipse.swt) {
          widgets.Canvas,
          widgets.Composite,
          events.DisposeListener,
          events.DisposeEvent,
          events.PaintListener,
          events.PaintEvent;
 }
If you're really using DWT and this is not just an example, you can import org.eclipse.swt.std to import the most common used modules or org.eclipse.swt.all to import all of them. -- /Jacob Carlborg
Apr 01 2015