generic-programming

Generic programming vs. Metaprogramming

Deadly 提交于 2019-11-29 22:28:48
What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming. in reference to C++. So apparently they're different? Programming : Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data. Metaprogramming : Writing a program that creates, transforms, filters, aggregates and otherwise manipulates programs

Boilerplate-free annotation of ASTs in Haskell?

独自空忆成欢 提交于 2019-11-29 22:19:03
I've been fiddling around with the Elm compiler, which is written in Haskell. I'd like to start implementing some optimizations for it, and part of this involves traversing the AST and adding "annotation" to certain nodes, such as tail-calls, etc. I know I can use SYB or uniplate to do the traversal, but I'm wondering if there's a boilerplate-free way to deal with the types. So, suppose we have a bunch of algebraic types for our AST: data Expr = PlusExpr Expr Expr ... data Def = TypeAlias String [String] Type ... If I were writing the boilerplate, I'd make new types like this: data

How to resolve generic type of property value from decorated property in decorator

吃可爱长大的小学妹 提交于 2019-11-29 17:40:27
I'm playing with some code, which resolve generic type of property value and doesn't allow provide wrong value. But when I change from TValue to (t: TValue) => TValue , type TValue doesn't going resolved more. It's now unknown {} type, not number more Example without function. Working good type ProtoOf<T> = Pick<T, keyof T>; function decorate<TValue>(value: TValue) { return <T extends { [KA in TKey]: TValue }, TKey extends keyof T>( proto: ProtoOf<T> & { [P in TKey]: TValue }, propertyKey: TKey ) => {}; } class Foo { // TS error: none // Result: EXPECTED @decorate(1) bar: number = 1; // TS

Generic Programming in Scala

依然范特西╮ 提交于 2019-11-29 16:55:19
Hi all I am fairly new to Scala coming from C#. I am attempting to write my own version of accumulate ( fold) I am wondering why I am running into some issues with the following: def accumulate[T](list : List[T], initial: T, f: (T, T) => T) : T = { @tailrec def loop[T](list: List[T], accum: T) : T = if(list.length == 0) accum else{ val head : T = list.head val res : T = f(accum,head) loop[T](list.tail, res) } loop(list,initial) } I am getting the following error: type mismatch; found : accum.type (with underlying type T) required: T val res : T = f(accum,head) ^ I cant see how I have a type

Arity-generic programming in Agda

走远了吗. 提交于 2019-11-29 14:42:10
问题 How to write arity-generic functions in Agda? Is it possible to write fully dependent and universe polymorphic arity-generic functions? 回答1: I'll take an n-ary composition function as an example. The simplest version open import Data.Vec.N-ary comp : ∀ n {α β γ} {X : Set α} {Y : Set β} {Z : Set γ} -> (Y -> Z) -> N-ary n X Y -> N-ary n X Z comp 0 g y = {!!} comp (suc n) g f = {!!} Here is how N-ary is defined in the Data.Vec.N-ary module: N-ary : ∀ {ℓ₁ ℓ₂} (n : ℕ) → Set ℓ₁ → Set ℓ₂ → Set (N

How can I produce a Tag type for any datatype for use with DSum, without Template Haskell?

不羁的心 提交于 2019-11-29 12:52:44
background I want to write some library code, which internally uses DSum to manipulate a user's datatype. DSum requires a 'tag' type that has a single type argument. However I want my code to work with just any old concrete type. So, I'd like to just take the user's type and automatically produce the tag type. I've asked a very similar question here How can I programatically produce this datatype from the other? , and gotten a great answer. That answer relies on TH, mainly so that it can create top-level declarations. However, I actually don't care about the top-level declaration, and I'd

How to make some generic programming in fortran 90/95 working with intrinsic types

感情迁移 提交于 2019-11-29 11:18:29
I would like to program some procedure that will work with different types. I am planning to use the "include" method used in flibs described here and here . I give here a simple exemple. ! -------------------------------------------------------------- ! module data_type type ivalue integer :: v end type type rvalue real(8) :: v end type end module data_type ! -------------------------------------------------------------- ! module imod use data_type, only: T => ivalue include "template.f90" end module imod ! -------------------------------------------------------------- ! module rmod use data

How much existing C++ code would break if void was actually defined as `struct void {};`

北战南征 提交于 2019-11-29 09:09:04
void is a bizarre wart in the C++ type system. It's an incomplete type that cannot be completed, and it has all sort of magic rules about the restricted ways it can be employed: A type cv void is an incomplete type that cannot be completed; such a type has an empty set of values. It is used as the return type for functions that do not return a value. Any expression can be explicitly converted to type cv void ([expr.cast]). An expression of type cv void shall be used only as an expression statement, as an operand of a comma expression, as a second or third operand of ?: ([expr.cond]), as the

Generic Types Collection

大兔子大兔子 提交于 2019-11-29 08:52:47
Building on previous question which got resolved, but it led to another problem. If protocol/class types are stored in a collection, retrieving and instantiating them back throws an error. a hypothetical example is below. The paradigm is based on "Program to Interface not an implementation" What does it mean to "program to an interface"? instantiate from protocol.Type reference dynamically at runtime public protocol ISpeakable { init() func speak() } class Cat : ISpeakable { required init() {} func speak() { println("Meow"); } } class Dog : ISpeakable { required init() {} func speak() {

How can I subtract two generic objects (T - T) in C# (Example: DateTime - DateTime)?

喜欢而已 提交于 2019-11-29 07:00:40
问题 I wrote a Generic Class : public class Interval<T> where T : IComparable // for checking that Start < End { public T Start { get; set; } public T End { get; set; } ... } And I use this class with DateTime, int, etc. I need a Duration property that returns a duration like: public object Duration { get { return End - Start; } } But when this property is included in my class, the compiler raises a logical error on the - operator. What can I do to achieve this goal normally, or should I ignore it