Skip to content

Instantly share code, notes, and snippets.

@zah
Created December 1, 2011 14:55
Show Gist options
  • Save zah/1417334 to your computer and use it in GitHub Desktop.
Save zah/1417334 to your computer and use it in GitHub Desktop.
Let - first tests
type
TBar = object
f: int
TFoo = object
str: string
sq: seq[string]
bar: TBar
proc makeFoo: TFoo =
result.sq = @["a", "b", "c"]
result.str = "d"
result.bar.f = 10
proc seqAccessor(x: TFoo): seq[string] =
return x.sq
proc strAccessor(x: TFoo): string =
return x.str
proc barAccessor(x: TFoo): TBar =
return x.bar
proc testWithUserDefinedType =
var str = "Hello"
var varObj = makeFoo()
let letObj = makeFoo()
# Both of these construct a new object like this:
# memset((void*)&Letobj_28044, 0, sizeof(Letobj_28044));
# memset((void*)&LOC2, 0, sizeof(LOC2));
# Makefoo_28010(&LOC2);
# memcpy((void*)&Letobj_28044, (NIM_CONST void*)&LOC2, sizeof(Letobj_28044));
#
# Why is the temporary location (LOC2) necessary here?
var varStr = str
# Varstr_28045 = copyString(Str_28042); (copy is made, correct)
let letStr = str
# Letstr_28046 = Str_28042; (only pointer copied, correct)
let let_as_alt_name = varObj
# This produces a new shallow copy instead of pointer:
# memset((void*)&Letasaltname_141051, 0, sizeof(Letasaltname_141051));
# memcpy((void*)&Letasaltname_141051, (NIM_CONST void*)&Varobj_141047, sizeof(Letasaltname_141051));
var var_as_alt_name = varObj
var var_as_alt_name_2 = letObj
# This uses genericAssign to produce a copy (correct):
# memset((void*)&Varasaltname_141052, 0, sizeof(Varasaltname_141052));
# genericAssign((void*)&Varasaltname_141052, (void*)&Varobj_141047, NTI141008);
let let_as_shared_subexpression_1 = letObj.sq
# only pointer is copied here (correct)
let let_as_shared_subexpression_2 = letObj.bar
# this uses standard C struct assignment to produce a copy
# Letassharedsubexpression2_141056 = Letobj_141048.Bar;
# the type of Letassharedsubexpression2_141056 is the TBar's struct type
var var_as_shared_subexpression_1 = letObj.sq
# uses genericSeqAssign (correct)
var var_as_shared_subexpression_2 = letObj.bar
# copy is made with standard C assignment just like with let above (correct)
# doesn't compile: let let_as_shared_subexpression_2 = letObj.seqAccessor
let let_as_shared_subexpression_3 = varObj.seqAccessor
let let_as_shared_subexpression_4 = varObj.barAccessor
let let_as_shared_subexpression_5 = varObj.strAccessor
# doesn't compile: var var_as_shared_subexpression_2 = letObj.seqAccessor
var var_as_shared_subexpression_3 = varObj.seqAccessor
var var_as_shared_subexpression_4 = varObj.barAccessor
var var_as_shared_subexpression_5 = varObj.strAccessor
# All of these produce the same code - Copy is made inside the accessor
# function and it is returned as a regular function return value:
# Letassharedsubexpression4_141058 = Baraccessor_141040(&Varobj_141047);
#
# I expected this since the location = copy(accessor()) behavior certainly
# requires more compilacated changes to the C generator.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment