Skip to content

Instantly share code, notes, and snippets.

@Deco
Deco / util.lua
Created January 24, 2012 20:43
Extensions to the default Lua functions for flexibility through metamethods
do -- base function reimplementations
do _G.rawtype = _G.rawtype or _G.type
local meta, override
function _G.type(v)
local vtype = rawtype(v)
if vtype ~= "table" and vtype ~= "userdata" then return vtype end
meta = debug.getmetatable(v)
if meta then
override = rawget(meta, "__type")
var x : auto = 10
# Error: internal error: getTypeDescAux(tyExpr)
# No stack traceback available
# To create a stacktrace, rerun compilation with ./koch temp c <file>
import unittest
type memory* [T=byte] = distinct ptr T
proc `+`*[T](mem: memory[T]; offset: int): memory[T] =
cast[memory[T]](cast[int](mem) + offset*sizeof(T))
proc `-`*[T](mem: memory[T]; offset: int): memory[T] =
cast[memory[T]](cast[int](mem) - offset*sizeof(T))
proc incval(x: string; y: string): string =
"completely unrelated"
proc test(): tuple[incval: proc(); getval: proc():int] =
var val = 0
proc incval() =
inc val
{.experimental.}
type Mat2F
[
rowCount, colCount: static[int];
elementType
] =
object ## A 2-dimensional fixed-sized matrix of an arbitrary type
elementList: array[rowCount*colCount, elementType]
@Deco
Deco / aahh.nim
Last active August 29, 2015 14:15
import unittest, typetraits
suite "aahh":
test "aahh":
var a = 5
let cond = (name(type(a)) == "int")
require(cond) # works
import sequtils
template test(x: int; list: varargs[int]): expr =
foldl(list, a*(b+x))
echo test(5, 1, 2, 3, 4) # 504
import memory
type MatD*[ElementT] = tuple
elementCount: uint32
dimensionCount: uint16
data: memory[byte]
template matD(dimensions: varargs[float64]; elementT: typedesc): expr =
type MatF[ElementT, PLEASE_HELP] = object
data: array[SOME_VARIABLE_LENGTH_HERE, ElementT]
template matF*(elementT: typedesc; dimensions: varargs[int]): expr =
let dimensionCount = len(dimensions)
let elementCount = foldl(dimensions, a*b)
???
type Test = tuple
x, y: int
const size = sizeof(Test) # Error: cannot evaluate 'sizeof(Test)'
echo size