I think the code and the error message explains it better:
import options, sequtils
type
Person = object
id: int
name: string
Spouse = object
base: ref Person
engagedTo: Option[ref Person]
preferences: seq[ref Person]
Population = object
males, females: seq[ref Spouse]
proc newPerson(name: string; isMale: bool): Person =
if isMale:
var nextMaleId {.global.} = 1
result.id = nextMaleId
inc nextMaleId
else:
var nextFemaleId {.global.} = -1
result.id = nextFemaleId
dec nextFemaleId
result.name = name
proc newSpouse(base: ref Person; preferences: openArray[ref Person]):
Spouse =
result.base = base
result.preferences = preferences.toSeq()
proc isSingle(spouse: ref Spouse): bool = spouse.engagedTo.isNone()
proc prefersMoreThanCurrent(baseSpouse: ref Spouse;
probePerson: ref Person): Option[bool] =
if baseSpouse.isSingle():
result = some(true)
else:
for refPerson in baseSpouse.preferences:
if refPerson.id == probePerson.id:
result = some(true)
break
if refPerson.id == baseSpouse.engagedTo.unsafeGet().id:
result = some(false)
break
proc newPopulation(males, females: openArray[ref Spouse]):
Option[Population] =
let dim = males.len()
if males.len() == females.len():
if males.allIt(it.preferences.len() == dim):
if females.allIt(it.preferences.len() == dim):
result = some[Population](Population(males: males.toSeq(),
females: females.toSeq()))
proc engage(male, female: var Spouse) =
male.engagedTo = some[ref Person](female.base)
female.engagedTo = some[ref Person](male.base)
proc solve(population: var Population): Option[void] =
for male in population.males:
for female in population.females:
let prefersMore = female.prefersMoreThanCurrent(male.base)
if prefersMore.isNone():
return result
elif prefersMore.unsafeGet():
engage(male, female)
Run
/home/u98982887/proj/cdsan/src/cdsan/stable_matching.nim(63, 23) Error:
type mismatch: got <ref Spouse, ref Spouse>
but expected one of:
proc engage(male, female: var Spouse)
first type mismatch at position: 1
required type for male: var Spouse
but expression 'male' is of type: ref Spouse
expression: engage(male, female)
Tip: 1 messages have been suppressed, use --verbose to show them.
Error: Execution failed with exit code 1
... Command: "/home/u98982887/.nimble/bin/nim" c --noNimblePath
-d:NimblePkgVersion=0.1.0 "-r" "--path:."
"/home/u98982887/proj/cdsan/tests/test1"
Run