protocoled icon indicating copy to clipboard operation
protocoled copied to clipboard

Use macrocache module

Open planetis-m opened this issue 5 years ago • 4 comments

By creating const registry of type CacheTable, the nesting can be avoided. But most importantly it'll allow declaring interface implementations across modules https://nim-lang.org/docs/macrocache.html

planetis-m avatar Aug 08 '20 10:08 planetis-m

Also see: https://github.com/mratsim/trace-of-radiance/blob/master/trace_of_radiance/support/emulate_classes_with_ADTs.nim

planetis-m avatar Aug 08 '20 10:08 planetis-m

Use new concept api once its finally merged: https://github.com/nim-lang/Nim/pull/15251

planetis-m avatar Feb 07 '21 22:02 planetis-m

next gen syntax:

type
  PExpr {.runtime.} = concept
    proc eval(x: Self): int

impl PExpr:
  type
    PLiteral = ref object
      x: int

  proc newLit(x: int): PLiteral =
    result = PLiteral(x: x)
  proc eval(x: PLiteral): int = x.x

impl PExpr:
  type
    PPlusExpr = ref object
      a, b: PExpr

  proc newPlus(a, b: PExpr): PPlusExpr =
    result = PPlusExpr(a: a, b: b)
  proc eval(x: PPlusExpr): int = eval(x.a) + eval(x.b)

#Also valid:
type
  PPlusExpr {.impl: PExpr.} = ref object
    a, b: PExpr

proc eval(x: PLiteral): int {.impl: PExpr.} = x.x

planetis-m avatar Mar 17 '21 09:03 planetis-m

#2 This is also valid:

type
  PPlusExpr {.impl: PExpr.} = concept
    var a, b: PExpr

    proc newPlus(a, b: PExpr): PPlusExpr =
      result = PPlusExpr(a: a, b: b)
    proc eval(x: PPlusExpr): int = eval(x.a) + eval(x.b)

planetis-m avatar Mar 17 '21 09:03 planetis-m