Skip to content

[RFC] partial generic deduction #86

Closed as not planned
Closed as not planned
@timotheecour

Description

@Araq @dom96 @siliconvoodoo

Nim should support partial generic deduction.

# D20180406T122342
import typetraits

proc test_IFTI() =
  ## full IFTI
  proc foo[T](a:T): auto = return a*a
  echo foo 10

  ## partial IFTI

  ### proc
  proc foo1[T, T2](a:T2): auto = return T.name & ":" & T2.name
  echo foo1[string, int](10)
  when false:
    echo foo1[string](10) # Error: cannot instantiate: foo1[string]; got 1 type(s) but expected 2
    echo 10.foo1[string]() #  Error: cannot instantiate: 'T'

  ## side issue: named generic parametes
  when false:
    echo foo1[T:string](10) #  Error: invalid expression: 'T: string'
    echo foo1[T:string, T2:int](10) #  Error: invalid expression: 'T: string'
    echo 10.foo1[T:string]() #  Error: cannot instantiate: 'T'

  ### template
  template foo2[T, T2](a:T2): auto = T.name & ":" & T2.name
  echo foo2[string, int](10)
  echo foo2[string](10)
  when false:
    echo 10.foo2[string] # Error: cannot instantiate: 'T'

  ## workaround with typedesc
  template foo3[T2](a:T2, T: typedesc): auto = T.name & ":" & T2.name
  echo 10.foo3(string)

test_IFTI()

in C++ this works:

template<typename T, typename T2>
void fun(const T2& a){}

void test(){
  fun<float>(10);
}

in D this works and used extensively

void fun(T, T2)(T2 a){}
void test(){
  fun!string(10); // same as fun!(string)(10)
}

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions