Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Functions inside type-class instances cannot reference each other #158

Open
dbousamra opened this issue Nov 28, 2012 · 0 comments
Open

Functions inside type-class instances cannot reference each other #158

dbousamra opened this issue Nov 28, 2012 · 0 comments
Labels

Comments

@dbousamra
Copy link
Collaborator

Take the following code. Notice how in the VectorFuncs instance, the mult, add, sub and div functions all partially apply the op function. Now I know I could just do it without the partial application and get rid of op altogether, but this is more fun.

type Vector = { x: Number, y: Number, z: Number }

typeclass VectorFuncs #a {
  op:   Function(Function(Number, Number, Number), #a, #a, #a)
  mult: Function(#a, #a, #a)
  add:  Function(#a, #a, #a)
  sub:  Function(#a, #a, #a)
  div:  Function(#a, #a, #a)
}

instance vectorsWithFuncs = VectorFuncs Vector {
  op:   \f vec1 vec2 -> { x: (f a.x b.x), y: (f a.y b.y), z: (f a.z b.z) }
  mult: \vec1 vec2 -> op (\x y -> x * y) vec1 vec2
  add:  \vec1 vec2 -> op (\x y -> x + y) vec1 vec2
  sub:  \vec1 vec2 -> op (\x y -> x - y) vec1 vec2
  div:  \vec1 vec2 -> op (\x y -> x / y) vec1 vec2
}

I receive the following error:

/Users/dominic/Documents/workspace/Roy/roy/src/compile.js:828
            output = vm.runInNewContext(compiled.output, sandbox, 'eval');
                        ^
TypeError: Object #<Object> has no method 'op'
    at Object.vectorsWithFuncs.mult (eval:10:28)
    at eval:31:30
    at main (/Users/domlebo70/Documents/workspace/Roy/roy/src/compile.js:828:25)
    at Array.forEach (native)
    at Function._.each._.forEach (/Users/domlebo70/Documents/workspace/Roy/roy/node_modules/underscore/underscore.js:74:11)
    at Object.main (/Users/domlebo70/Documents/workspace/Roy/roy/src/compile.js:803:7)
    at Object.<anonymous> (/Users/domlebo70/Documents/workspace/Roy/roy/roy:2:26)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant