Dynamically set local variables in Ruby [duplicate]

As an additional information for future readers, starting from ruby 2.1.0 you can using binding.local_variable_get and binding.local_variable_set: def foo a = 1 b = binding b.local_variable_set(:a, 2) # set existing local variable `a’ b.local_variable_set(:c, 3) # create new local variable `c’ # `c’ exists only in binding. b.local_variable_get(:a) #=> 2 b.local_variable_get(:c) #=> 3 p a … Read more

What is the purpose of C++20 std::common_reference?

common_reference came out of my efforts to come up with a conceptualization of STL’s iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type. The former is the return type of the iterator’s operator*, and the value_type is the (non-const, non-reference) type of the elements of … Read more

Template Metaprogramming – Difference Between Using Enum Hack and Static Const

Enums aren’t lvals, static member values are and if passed by reference the template will be instanciated: void f(const int&); f(TMPFib<1>::value); If you want to do pure compile time calculations etc. this is an undesired side-effect. The main historic difference is that enums also work for compilers where in-class-initialization of member values is not supported, … Read more

How can I dynamically create class methods for a class in python [duplicate]

You can dynamically add a classmethod to a class by simple assignment to the class object or by setattr on the class object. Here I’m using the python convention that classes start with capital letters to reduce confusion: # define a class object (your class may be more complicated than this…) class A(object): pass # … Read more

How to access parameter list of case class in a dotty macro

Using standard type class derivation in Dotty import scala.deriving.Mirror case class ParseError(str: String, msg: String) trait Decoder[T]{ def decode(str:String): Either[ParseError, T] } object Decoder { given Decoder[String] with { override def decode(str: String): Either[ParseError, String] = Right(str) } given Decoder[Int] with { override def decode(str: String): Either[ParseError, Int] = str.toIntOption.toRight(ParseError(str, “value is not valid Int”)) … Read more

Dynamic/runtime method creation (code generation) in Python

Based on Theran’s code, but extending it to methods on classes: class Dynamo(object): pass def add_dynamo(cls,i): def innerdynamo(self): print “in dynamo %d” % i innerdynamo.__doc__ = “docstring for dynamo%d” % i innerdynamo.__name__ = “dynamo%d” % i setattr(cls,innerdynamo.__name__,innerdynamo) for i in range(2): add_dynamo(Dynamo, i) d=Dynamo() d.dynamo0() d.dynamo1() Which should print: in dynamo 0 in dynamo 1

Compile time sizeof_array without using a macro

Try the following from here: template <typename T, size_t N> char ( &_ArraySizeHelper( T (&array)[N] ))[N]; #define mycountof( array ) (sizeof( _ArraySizeHelper( array ) )) int testarray[10]; enum { testsize = mycountof(testarray) }; void test() { printf(“The array count is: %d\n”, testsize); } It should print out: “The array count is: 10”