-
-
Save define-private-public/88dd50f80a01d11ee1444391fba6d06c to your computer and use it in GitHub Desktop.
nim interface sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defining an interface | |
# Would produce something like the IWorker below. | |
interface IWorker: | |
setup: proc(a, b: float) | |
run: proc() | |
getResult(): float | |
# `implements` syntax might look like this | |
Adder implements IWorker: | |
setup = Adder_setup | |
run = Adder_run | |
getResult = Adder_getResult | |
# Then could pass it like this | |
var worker:IWorker = Adder | |
worker.obj = ... | |
# Do the work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type | |
# The interface | |
IWorker* = object | |
obj*: ref RootObj | |
setup_proc*: proc(obj: ref RootObj, a, b: float) | |
run_proc*: proc(obj: ref RootObj) | |
getResult_proc*: proc(obj: ref RootObj): float | |
# A base type | |
MathOperation = object of RootObj | |
result: float | |
Adder = ref object of MathOperation | |
first, second: float | |
Subtracter = ref object of MathOperation | |
initial, subtraction: float | |
# Multiplier = ref object of MathOperation | |
# value, multiplier: float | |
# | |
# Divider = ref object of MathOperation | |
# numerator, divisor: float | |
#=== Procs for the IWorker ===# | |
proc setup(worker: IWorker; a, b: float) = | |
worker.setup_proc(worker.obj, a, b) | |
proc run(worker: IWorker) = | |
worker.run_proc(worker.obj) | |
proc getResult(worker: IWorker): float = | |
return worker.getResult_proc(worker.obj) | |
#=== Procs for the Adder ===# | |
proc Adder_setup(obj: ref RootObj; a, b: float) = | |
var this = cast[Adder](obj) | |
this.first = a | |
this.second = b | |
proc Adder_run(obj: ref RootObj) = | |
var this = cast[Adder](obj) | |
this.result = this.first + this.second | |
proc Adder_getResult(obj: ref RootObj): float = | |
var this = cast[Adder](obj) | |
return this.result | |
#=== Procs for the Subtractter ===# | |
proc Subtracter_setup(obj: ref RootObj; a, b: float) = | |
var this = cast[Subtracter](obj) | |
this.initial = a | |
this.subtraction = b | |
proc Subtracter_run(obj: ref RootObj) = | |
var this = cast[Subtracter](obj) | |
this.result = this.initial - this.subtraction | |
proc Subtracter_getResult(obj: ref RootObj): float = | |
var this = cast[Subtracter](obj) | |
return this.result | |
var | |
worker: IWorker | |
# Slap together the interface | |
add = IWorker( | |
setup_proc: Adder_setup, | |
run_proc: Adder_run, | |
getResult_proc: Adder_getResult | |
) | |
sub = IWorker( | |
setup_proc: Subtracter_setup, | |
run_proc: Subtracter_run, | |
getResult_proc: Subtracter_getResult | |
) | |
# Assign an object (the objects in this case really don't have any special data) | |
add.obj = new(Adder) | |
sub.obj = new(Subtracter) | |
# Try out the workers | |
worker = add | |
worker.setup(10.0, 2.0) | |
worker.run | |
echo worker.getResult # Should be 12 | |
worker = sub | |
worker.setup(10.0, 2.0) | |
worker.run | |
echo worker.getResult # Should be 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment