Search Results

Search found 175 results on 7 pages for 'metaprogramming'.

Page 6/7 | < Previous Page | 2 3 4 5 6 7  | Next Page >

  • Python metaclass to run a class method automatically on derived class

    - by Barry Steyn
    I want to automatically run a class method defined in a base class on any derived class during the creation of the class. For instance: class Base(object): @classmethod def runme(): print "I am being run" def __metclass__(cls,parents,attributes): clsObj = type(cls,parents,attributes) clsObj.runme() return clsObj class Derived(Base): pass: What happens here is that when Base is created, ''runme()'' will fire. But nothing happens when Derived is created. The question is: How can I make ''runme()'' also fire when creating Derived. This is what I have thought so far: If I explicitly set Derived's metclass to Base's, it will work. But I don't want that to happen. I basically want Derived to use the Base's metaclass without me having to explicitly set it so.

    Read the article

  • Disallow taking pointer/reference to const to a temporary object in C++ (no C++0X)

    - by KRao
    Hi, I am faced with the following issue. Consider the following class: //Will be similar to bost::reference_wrapper template<class T> class Ref { public: explicit Ref(T& t) : m_ptr(&t) {} private: T* m_ptr; }; and this function returning a double double fun() {return 1.0;} If we now have double x = 1.0; const double xc = 1.0; Ref<double> ref1(x); //OK Ref<const double> refc1(cx); //OK good so far, however: //Ref<double> ref2( fun() ); //Fails as I want it to Ref<const double> refc2( fun() ); //Works but I would like it not to Is there a way to modify Ref (the way you prefer) but not the function fun, so that the last line returns a compile-time error? Please notice you can modify the constructor signature (as long as I am able to initialise the Ref as intended). Thank you in advance for your help!

    Read the article

  • Debugging metaprograms [C++]

    - by atch
    Hi, Is there any way to check step by step what's going on in let's say template? I mean how it is instantiated step by step and so on? In book I've mentioned here , I found (2 minutes ago) quite interesting example of how binary could be implemented as a metafunction. template <unsigned long N> struct binary { static unsigned const value = binary<N/10>::value << 1 // prepend higher bits | N%10; // to lowest bit }; template <> // specialization struct binary<0> // terminates recursion { static unsigned const value = 0; }; and I think it could be quite useful to be able to see step by step what's been done during the instantiation of this template. Thanks for your replies.

    Read the article

  • C# Generic Generics (A Serious Question)

    - by tahirhassan
    In C# I am trying to write code where I would be creating a Func delegate which is in itself generic. For example the following (non-Generic) delegate is returning an arbitrary string: Func<string> getString = () => "Hello!"; I on the other hand want to create a generic which acts similarly to generic methods. For example if I want a generic Func to return default(T) for a type T. I would imagine that I write code as follows: Func<T><T> getDefaultObject = <T>() => default(T); Then I would use it as getDefaultObject<string>() which would return null and if I were to write getDefaultObject<int>() would return 0. This question is not merely an academic excercise. I have found numerous places where I could have used this but I cannot get the syntax right. Is this possible? Are there any libraries which provide this sort of functionality?

    Read the article

  • C++: How to require that one template type is derived from the other

    - by Will
    In a comparison operator: template<class R1, class R2> bool operator==(Manager<R1> m1, Manager<R2> m2) { return m1.internal_field == m2.internal_field; } Is there any way I could enforce that R1 and R2 must have a supertype or subtype relation? That is, I'd like to allow either R1 to be derived from R2, or R2 to be derived from R1, but disallow the comparison if R1 and R2 are unrelated types.

    Read the article

  • "Inlining" (kind of) functions at runtime in C

    - by fortran
    Hi, I was thinking about a typical problem that is very JIT-able, but hard to approach with raw C. The scenario is setting up a series of function pointers that are going to be "composed" (as in maths function composition) once at runtime and then called lots and lots of times. Doing it the obvious way involves many virtual calls, that are expensive, and if there are enough nested functions to fill the CPU branch prediction table completely, then the performance with drop considerably. In a language like Lisp, I could probably process the code and substitute the "virtual" call by the actual contents of the functions and then call compile to have an optimized version, but that seems very hacky and error prone to do in C, and using C is a requirement for this problem ;-) So, do you know if there's a standard, portable and safe way to achieve this in C? Cheers

    Read the article

  • Scope of Groovy's ExpandoMetaClass?

    - by TicketMonster
    Groovy exposes an ExpandoMetaClass that allows you to dynamically add instance and class methods/properties to a POJO. I would like to use it to add an instance method to one of my Java classes: public class Fizz { // ...etc. } Fizz fizz = new Fizz(); fizz.metaClass.doStuff = { String blah -> fizz.buzz(blah) } This would be the equivalent to refactoring the Fizz class to have: public class Fizz { // ctors, getters/setters, etc... public void doStuff(String blah) { buzz(blah); } } My question: Does this add doStuff(String blah) to only this particular instance of Fizz? Or do all instances of Fizz now have a doStuff(String blah) instance method? If the former, how do I get all instances of Fizz to have the doStuff instance method? I know that if I made the Groovy: fizz.metaClass.doStuff << { String blah -> fizz.buzz(blah) } Then that would add a static class method to Fizz, such as Fizz.doStuff(String blah), but that's not what I want. I just want all instances of Fizz to now have an instance method called doStuff. Ideas?

    Read the article

  • specyfic syntax question

    - by bua
    Hi there, Is it possible to create template to the initialization like: template <typename C> typename C::value_type fooFunction(C& c) {...}; std::vector<string> vec_instance; fooFunction(cont<0>(vec_instance)); fooFunction(cont<1>(vec_instance)); In general i'm interested is it possible to specify template using integer (ie. 0) instead of true type name. And how to achieve above?

    Read the article

  • How can I iterate through all of the Models in my rails app?

    - by James
    I would like to be able to iterate over and inspect all the models in my rails app. In pseudo-code it would look something like: rails_env.models.each do |model| associations = model.reflect_on_all_associations(:has_many) ... do some stuff end My question is how do I inspect my rails app to get a collection of the models (rails_env.models) ?

    Read the article

  • Checking if a function has C-linkage at compile-time [unsolvable]

    - by scjohnno
    Is there any way to check if a given function is declared with C-linkage (that is, with extern "C") at compile-time? I am developing a plugin system. Each plugin can supply factory functions to the plugin-loading code. However, this has to be done via name (and subsequent use of GetProcAddress or dlsym). This requires that the functions be declared with C-linkage so as to prevent name-mangling. It would be nice to be able to throw a compiler error if the referred-to function is declared with C++-linkage (as opposed to finding out at runtime when a function with that name does not exist). Here's a simplified example of what I mean: extern "C" void my_func() { } void my_other_func() { } // Replace this struct with one that actually works template<typename T> struct is_c_linkage { static const bool value = true; }; template<typename T> void assertCLinkage(T *func) { static_assert(is_c_linkage<T>::value, "Supplied function does not have C-linkage"); } int main() { assertCLinkage(my_func); // Should compile assertCLinkage(my_other_func); // Should NOT compile } Is there a possible implementation of is_c_linkage that would throw a compiler error for the second function, but not the first? I'm not sure that it's possible (though it may exist as a compiler extension, which I'd still like to know of). Thanks.

    Read the article

  • Enhance Predfined Methods in Scala

    - by fratnk
    Base question: Why can I write in Scala just: println(10) Why don't I need to write: Console println(10) Followup question: How can I introduce a new method "foo" which is everywhere visible and usable like "println"?

    Read the article

  • C++ require that one template type is derived from the other

    - by Will
    In a comparison operator: template<class R1, class R2> bool operator==(Manager<R1> m1, Manager<R2> m2) { return p1.internal_field == p2.internal_field; } Is there any way I could enforce that R1 and R2 must have a supertype or subtype relation? That is, I'd like to allow either R1 to be derived from R2, or R2 to be derived from R1, but disallow the comparison if R1 and R2 are unrelated types.

    Read the article

  • Edd strikes again &ndash; IronRuby for Rubyists on InfoQ

    - by Eric Nelson
    Colleague, friend and generally top guy on IronRuby Edd Morgan has just been published over on InfoQ. To wet the appetite… a snippet or three. IronRuby for Rubyists IronRuby is Microsoft's implementation of the Ruby language we all know and love with the added bonus of interoperability with the .NET framework — the Iron in the name is actually an acronym for 'Implementation running on .NET'. It's supported by the .NET Common Language Runtime as well as, albeit unofficially, the Mono project. You'd be forgiven for harbouring some question in your mind about running a dynamic language such as Ruby atop the CLR - that's where the DLR (Dynamic Language Runtime) comes in. The DLR is Microsoft's way of providing dynamic language capability on top of the CLR. Both IronRuby and the DLR are, as part of Microsoft's commitment to open source software, available as part of the Microsoft Public License on GitHub and CodePlex respectively… And Metaprogramming with IronRuby The art and science of metaprogramming — especially in Ruby, where it's an absolute joy — is something that could very easily span an entire article. As you would hope, IronRuby code is fully able to manipulate itself allowing you to bend your classes to your whim just as you would expect with a good dynamic language… And Riding the irails? So let's get to the point. I think it's a solid bet to make that a large proportion of Ruby programmers are familiar with the Rails framework - perhaps it's even safe to assume that most were first led to the Ruby language by the siren song of the Rails framework itself. Long story short, IronRuby is compatible enough to run your Rails app… Now… get yourself over to the full article and also check out some of Edds other work below. Related Links: 5 Steps to getting started with IronRuby Mini Book Review of IronRuby Unleashed by Shay Friedman Guest Post: Using IronRuby and .NET to produce the ‘Hello World of WPF’ – also by Edd Getting PhP and Ruby working on Windows Azure and SQL Azure Guest Post: What's IronRuby, and how do I put it on Rails? – also by Edd

    Read the article

  • How to persuade C fanatics to work on my C++ open source project?

    - by paperjam
    I am launching an open-source project into a space where a lot of the development is still done Linux-kernel-style, i.e. C-language with a low-level mindset. There are multiple benefits to C++ in our space but I fear those used to working in C will be scared off. How can I make the case for the benefits of C++? Specifically, the following C++ attributes are very valuable: concept of objects and reference-counting pointers - really don't want to have to malloc(sizeof(X)) or memcpy() structs templates for specialising whole bodies of code with specific performance optimizations and for avoiding duplication of code. template metaprogramming related to the above syntactic sweetness available (e.g. operator overloading, to be used in very small doses) STL Boost libraries Many of the knee-jerk negative reactions to C++ are illfounded. Performance does not suffer: modern compilers can flatten dozens of call stack levels and avoid bloat through wide use of template specializations. Granted, when using metaprogramming and building multiple specializations of a large call tree, compile time is slower but there are ways to mitigate this. How can I sell C++?

    Read the article

  • Ruby types of collections in ActiveRecord

    - by kmorris511
    If I have an object with a collection of child objects in ActiveRecord, i.e. class Foo < ActiveRecord::Base has_many :bars, ... end and I attempt to run Array's find method against that collection: foo_instance.bars.find { ... } I receive: ActiveRecord::RecordNotFound: Couldn't find Bar without an ID I assume this is because ActiveRecord has hijacked the find method for its own purposes. Now, I can use detect and everything is fine. However to satisfy my own curiousity, I attempted to use metaprogramming to explicitly steal the find method back for one run: unbound_method = [].method('find').unbind unbound_method.bind(foo_instance.bars).call { ... } and I receive this error: TypeError: bind argument must be an instance of Array so clearly Ruby doesn't think foo_instance.bars is an Array and yet: foo_instance.bars.instance_of?(Array) -> true Can anybody help me with an explanation of this and of a way to get around it with metaprogramming?

    Read the article

  • Generic callbacks

    - by bobobobo
    Extends So, I'm trying to learn template metaprogramming better and I figure this is a good exercise for it. I'm trying to write code that can callback a function with any number of arguments I like passed to it. // First function to call int add( int x, int y ) ; // Second function to call double square( double x ) ; // Third func to call void go() ; The callback creation code should look like: // Write a callback object that // will be executed after 42ms for "add" Callback<int, int, int> c1 ; c1.func = add ; c1.args.push_back( 2 ); // these are the 2 args c1.args.push_back( 5 ); // to pass to the "add" function // when it is called Callback<double, double> c2 ; c2.func = square ; c2.args.push_back( 52.2 ) ; What I'm thinking is, using template metaprogramming I want to be able to declare callbacks like, write a struct like this (please keep in mind this is VERY PSEUDOcode) <TEMPLATING ACTION <<ANY NUMBER OF TYPES GO HERE>> > struct Callback { double execTime ; // when to execute TYPE1 (*func)( TYPE2 a, TYPE3 b ) ; void* argList ; // a stored list of arguments // to plug in when it is time to call __func__ } ; So for when called with Callback<int, int, int> c1 ; You would automatically get constructed for you by < HARDCORE TEMPLATING ACTION > a struct like struct Callback { double execTime ; // when to execute int (*func)( int a, int b ) ; void* argList ; // this would still be void*, // but I somehow need to remember // the types of the args.. } ; Any pointers in the right direction to get started on writing this?

    Read the article

  • How do I write a good talk proposal for a FOSS programming conference?

    - by Andrew Grimm
    I'm hoping to give a talk at RubyKaigi this year, and I'd like to know what makes a good talk proposal. RubyKaigi is a conference run by Ruby enthusiasts (as opposed to it being a trade conference, or an academic conference). The proposal form can be seen here. So far, my draft proposal about a program I'm working on mentions: What the program is useful for and why it is relevant. How it works. What topics it touches upon (such as metaprogramming and testing) Is there anything that I should mention in my proposal? Also, how thorough should I be in my "Details of your talk" section? Should I be exhaustive, or only have a couple of short paragraphs?

    Read the article

  • Motivation Problems, Middle School Programmer [closed]

    - by Anonymous
    I'm in middle school at the moment and have been programming for about a year and a half. I mostly work with Python and Ruby, and am currently learning Rails. I know, you can never learn enough, it takes a looong time to master a subject, but I feel like I don't have much left to learn :(. I've learned many concepts in Python, learned basically the whole std lib and have written a ton of programs, same with Ruby. In Ruby I've also done a lot of metaprogramming. After I've learned all the concepts, and written a lot of programs, there is nothing really left for me to do! What can I do, now that I've learned all the concepts, and written some programs? I can't get a job working with real developers, and the programming camp I went to last year was far too easy.

    Read the article

  • Why Java as a First Language?

    - by dsimcha
    Why is Java so popular as a first language to teach beginners? To me it seems like a terrible choice: It's statically typed. Static typing isn't useful unless you care a lot about either performance or scaling to large projects. It requires tons of boilerplate to get the simplest code up and running. Try explaining "Hello, world" to someone who's never programmed before. It only handles the middle levels of abstraction well and is single-paradigm, thus leaving out a lot of important concepts. You can't program at a very low level (pointers, manual memory management) or a very high level, (metaprogramming, macros) in it. In general, Java's biggest strength (i.e. the reason people use it despite the shortcomings of the language per se) is its libraries and tool support, which is probably the least important attribute for a beginner language. In fact, while useful in the real world these may negatives from a pedagogical perspective as they can discourage learning to write code from scratch.

    Read the article

  • Is there a name for this functional programming construct/pattern?

    - by dietbuddha
    I wrote a function and I'd like to find out if it is an implementation of some functional programming pattern or construct. I'd like to find out the name of this pattern or construct (if it exists)? I have a function which takes a list of functions and does this to them: wrap(fn1, fn2, fn3, fn4) # returns partial(fn4, partial(fn3, partial(fn2, fn1))) There are strong similarities to compose, reduce, and other fp metaprogramming constructs, since the functions are being arranged together and returned as one function. It also has strong similarities to decorators and Python context managers since it provides a way to encapsulate pre and post execution behaviors in one function. Which was the impetus for writing this function. I wanted the ability that context managers provide, but I wanted to be able to have it defined in one function, and to be able to layer function after function on top.

    Read the article

< Previous Page | 2 3 4 5 6 7  | Next Page >