metaprogramming

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

南笙酒味 提交于 2020-07-18 06:31:05
问题 I am trying to learn meta-programming in dotty. Specifically compile time code generation. I thought learning by building something would be a good approach. So I decided to make a CSV parser which will parse lines into case classes. I want to use dotty macros to generate decoders trait Decoder[T]{ def decode(str:String):Either[ParseError, T] } object Decoder { inline given stringDec as Decoder[String] = new Decoder[String] { override def decode(str: String): Either[ParseError, String] =

Defining an Abstract model with a ForeignKey to another Abstract model

ぃ、小莉子 提交于 2020-07-07 07:43:20
问题 I'm trying to build two abstract classes called SurveyQuestionBase and SurveyResponseBase that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase model, when made concrete, should define a ForeignKey to a concrete model of SurveyQuestionBase . Django does not allow us to define ForeignKeys to abstract classes so I cannot, for instance, do this: question = models

Defining an Abstract model with a ForeignKey to another Abstract model

淺唱寂寞╮ 提交于 2020-07-07 07:41:22
问题 I'm trying to build two abstract classes called SurveyQuestionBase and SurveyResponseBase that will serve as templates to quickly define new concrete Models for implementing specific surveys on our website. The issue I am having is in enforcing that the SurveyResponseBase model, when made concrete, should define a ForeignKey to a concrete model of SurveyQuestionBase . Django does not allow us to define ForeignKeys to abstract classes so I cannot, for instance, do this: question = models

Haskell overlapping instances and type functions

梦想的初衷 提交于 2020-06-27 07:24:28
问题 I have the following typeclass which models a SQL-like query optimization: class OptimizableQuery q where type Optimized q :: * optimize :: q -> Optimized q instance Query q => OptimizableQuery q where type Optimized q = q optimize q = q instance (Query q, OptimizableQuery q) => OptimizableQuery (Select (Select q p) p) where type Optimized (Select (Select q p) p) = Select (Optimized q) p optimize (Select (Select q _) p) = Select (optimize q) p the problem is that I get the error "Conflicting

Understanding __call__ with metaclasses [duplicate]

别说谁变了你拦得住时间么 提交于 2020-06-24 09:54:14
问题 This question already has an answer here : need to understand the flow of __init__, __new__ and __call__ (1 answer) Closed 2 years ago . From my understanding the __call__ method inside a class implements the function call operator, for example: class Foo: def __init__(self): print("I'm inside the __init__ method") def __call__(self): print("I'm inside the __call__ method") x = Foo() #outputs "I'm inside the __init__ method" x() #outputs "I'm inside the __call__ method" However, I'm going

Register subclass to an ABC class inside __init_subclass__ does not fully work

。_饼干妹妹 提交于 2020-06-16 07:56:51
问题 What I want to achieve is to register one type as subtype of all other types. For some other reason I cannot use metaclass, so __init_subclass__ seems like a reasonable choice. I have code like this from abc import ABC class AnyData(ABC): pass class BaseData(ABC): def __init_subclass__(cls, **kwargs): super().__init_subclass__(**kwargs) cls.register(AnyData) class DataA(BaseData): pass However issubclass(AnyData, DataA) returns False , until the DataA is subclassed, like class DataB(DataA):

constexpr version of ::std::function

孤街浪徒 提交于 2020-06-16 02:50:11
问题 I am in search of a ::std::function usable in constexpr. Use case: I have a function which takes a function pointer as an argument, and a second which passes a lambda to the first function. Both are fully executable at compile time, so I want to constexpr them. Eg: template <class _Type> class ConstexprFunctionPtr { private: using Type = typename ::std::decay<_Type>::type; const Type function; public: constexpr inline ConstexprFunctionPtr(const Type f) : function(f) { } template <typename...

Difference between __callee__ and __method__

南楼画角 提交于 2020-06-10 07:33:06
问题 In Ruby, one can use either __callee__ or __method__ to find the name of the currently executing method. What is the difference between the two? 回答1: __method__ looks up the name statically, it refers to the name of the nearest lexically enclosing method definition. __callee__ looks up the name dynamically, it refers to the name under which the method was called. Neither of the two necessarily needs to correspond to the message that was originally sent: class << (foo = Object.new) def bar(*)

Is there a way to get type of template class from its complete type?

我的梦境 提交于 2020-05-12 04:57:05
问题 I need a meta-function that for given complete class type returns its template (e.g. f<foo<bar>>::type or f<foo<baz>>::type results in foo ). Or it may return true on f<foo<bar>, foo<baz>>::value and false on f<foo<bar>, not_foo<baz>>::value P.S: this was meant to be used with many chrono::duration like classes (but for weight units, mass units and so on). I needed different units not to convert one to another. 回答1: f<foo<bar>>::type or f<foo<baz>>::type results in foo Not exactly (see is-an

Ruby: Inherit code that works with class variables

﹥>﹥吖頭↗ 提交于 2020-04-23 10:18:46
问题 The situation: I have multiple classes that should each hold a variable with a configuration hash; a different hash for each class but the same for all instances of a class. At first, i tried like this class A def self.init config @@config = config end def config @@config end end class B < A; end class C < A; end But soon noticed that it wouldn't work that way because @@config is held in the context of A, not B or C, thus: B.init "bar" p B.new.config # => "bar" p C.new.config # => "bar" -