From 4cabf5dc5d0c36f8f26dd0f718498bcb9bd0d994 Mon Sep 17 00:00:00 2001 From: itsumura-h <39766805+itsumura-h@users.noreply.github.com> Date: Thu, 23 May 2024 04:42:21 +0900 Subject: [PATCH] fix (#11) --- README.md | 16 ++++++++-------- interface_implements.nimble | 2 +- src/interface_implements.nim | 10 ++++++---- tests/interface_implements/mock_repository.nim | 6 +++--- tests/interface_implements/repository.nim | 2 +- .../repository_interface.nim | 4 ++-- tests/interface_implements/usecase.nim | 4 ++-- 7 files changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index e8cea2a..83375c3 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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) ) ``` @@ -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) ) @@ -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 ``` @@ -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() @@ -106,7 +106,7 @@ repository.nim import interface_implements import ./repository_interface -type Repository = ref object +type Repository = object proc newRepository*():Repository = return Repository() @@ -120,7 +120,7 @@ usecase.nim ```nim import ./repository_interface -type Usecase = ref object +type Usecase = object repository: IRepository func newUsecase*(repository:IRepository):Usecase = diff --git a/interface_implements.nimble b/interface_implements.nimble index 67b36d3..51fc4c1 100644 --- a/interface_implements.nimble +++ b/interface_implements.nimble @@ -1,6 +1,6 @@ # Package -version = "0.2.5" +version = "0.2.6" author = "dumblepy" description = "Creating toInterface macro." license = "MIT" diff --git a/src/interface_implements.nim b/src/interface_implements.nim index 1ecd54c..4046299 100644 --- a/src/interface_implements.nim +++ b/src/interface_implements.nim @@ -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 @@ -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 # ================================================================================ @@ -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) diff --git a/tests/interface_implements/mock_repository.nim b/tests/interface_implements/mock_repository.nim index d5e5fde..54ba319 100644 --- a/tests/interface_implements/mock_repository.nim +++ b/tests/interface_implements/mock_repository.nim @@ -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() diff --git a/tests/interface_implements/repository.nim b/tests/interface_implements/repository.nim index f621108..1af51ed 100644 --- a/tests/interface_implements/repository.nim +++ b/tests/interface_implements/repository.nim @@ -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() diff --git a/tests/interface_implements/repository_interface.nim b/tests/interface_implements/repository_interface.nim index 5a57615..18a0d60 100644 --- a/tests/interface_implements/repository_interface.nim +++ b/tests/interface_implements/repository_interface.nim @@ -1,2 +1,2 @@ -type IRepository* = tuple - exec: proc(msg:string):string +type IRepository* = object + exec*: proc(msg:string):string diff --git a/tests/interface_implements/usecase.nim b/tests/interface_implements/usecase.nim index a74a5aa..674ffa1 100644 --- a/tests/interface_implements/usecase.nim +++ b/tests/interface_implements/usecase.nim @@ -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 =