Skip to content

Commit

Permalink
fix (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
itsumura-h authored May 22, 2024
1 parent 70b25ad commit 4cabf5d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ interface-implements

![](https://github.com/itsumura-h/nim-interface-implements/workflows/Build%20and%20test%20Nim/badge.svg)

There are two ways to achieve polymorphism in Nim. One is to create `tuple` and another is `dynamic dispatch`.
There are two ways to achieve polymorphism in Nim. One is to create `toInterface converter` and another is `dynamic dispatch`.

**tuple**
```nim
type IRepository* = tuple
type IRepository* = object
exec: proc(msg:string):string
```

Expand All @@ -20,7 +20,7 @@ proc exec(self:Repository, msg:string):string =
return &"Repository {msg}"
converter toInterface*(self:Repository):IRepository =
return (
return IRepository(
exec: proc(msg:string):string = self.exec(msg)
)
```
Expand Down Expand Up @@ -68,7 +68,7 @@ proc func2(self:Repository, number:int):string =
return "Repository2 " & $number
converter toInterface*(self:Repository):IRepository =
return (
return IRepository(
func1: proc(msg:string):string = self.func1(msg),
func2: proc(number:int):string = self.func2(number)
)
Expand All @@ -82,7 +82,7 @@ macro implements*(implName, interfaceName, procs:untyped):untyped
### Example
repository_interface.nim
```nim
type IRepository* = tuple
type IRepository* = object
exec: proc(msg:string):string
```

Expand All @@ -91,7 +91,7 @@ mock_repository.nim
import interface_implements
import ./repository_interface
type MockRepository = ref object
type MockRepository = object
proc newMockRepository*():MockRepository =
return MockRepository()
Expand All @@ -106,7 +106,7 @@ repository.nim
import interface_implements
import ./repository_interface
type Repository = ref object
type Repository = object
proc newRepository*():Repository =
return Repository()
Expand All @@ -120,7 +120,7 @@ usecase.nim
```nim
import ./repository_interface
type Usecase = ref object
type Usecase = object
repository: IRepository
func newUsecase*(repository:IRepository):Usecase =
Expand Down
2 changes: 1 addition & 1 deletion interface_implements.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.5"
version = "0.2.6"
author = "dumblepy"
description = "Creating toInterface macro."
license = "MIT"
Expand Down
10 changes: 6 additions & 4 deletions src/interface_implements.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro implements*(implName, interfaceName, procs:untyped):untyped =
let procName = aProc[0]
let procImpl = aProc[3]
if procName.repr.contains("*"):
raise newException(Exception, "procedure in interface must be private.")
raise newException(Exception, "procedure in implementation must be private.")
let returnType = if procImpl[0].repr.len > 0: ":" & procImpl[0].repr else: ""
for arg in procImpl[2..^1]:
let argName = arg[0].repr
Expand All @@ -26,14 +26,16 @@ macro implements*(implName, interfaceName, procs:untyped):untyped =
let resultStr =
if tuples.len == 0:
fmt"""converter toInterface*(self:{implName.repr}):{interfaceName.repr} =
return ()
return {interfaceName.repr}()
"""
else:
fmt"""converter toInterface*(self:{implName.repr}):{interfaceName.repr} =
return (
return {interfaceName.repr}(
{tuples}
)
"""
# echo ""
# echo (procsStr & '\n' & resultStr)
return (procsStr & '\n' & resultStr).parseStmt

# ================================================================================
Expand Down Expand Up @@ -76,5 +78,5 @@ when NimMajor >= 2:
methods.add(methodRow)
let interfaceStr = if i == 0: &"type {interfaceDef}\n" else: &"\ntype {interfaceDef}\n"
res.add(interfaceStr & methods.join("\n") & "\n" )
when not defined(release): echo res
# echo res
return parseStmt(res)
6 changes: 3 additions & 3 deletions tests/interface_implements/mock_repository.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import strformat
import repository_interface
import std/strformat
import ./repository_interface
import ../../src/interface_implements

type MockRepository* = ref object
type MockRepository* = object

proc new*(_:type MockRepository):MockRepository =
return MockRepository()
Expand Down
2 changes: 1 addition & 1 deletion tests/interface_implements/repository.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import strformat
import ./repository_interface
import ../../src/interface_implements

type Repository* = ref object
type Repository* = object

proc new*(_:type Repository):Repository =
return Repository()
Expand Down
4 changes: 2 additions & 2 deletions tests/interface_implements/repository_interface.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
type IRepository* = tuple
exec: proc(msg:string):string
type IRepository* = object
exec*: proc(msg:string):string
4 changes: 2 additions & 2 deletions tests/interface_implements/usecase.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import repository_interface
import ./repository_interface

type Usecase* = ref object
type Usecase* = object
repository: IRepository

func new*(_:type Usecase, repository:IRepository):Usecase =
Expand Down

0 comments on commit 4cabf5d

Please sign in to comment.