Are Erlang -callbacks to be invoked only through MFA functions (apply/3, spawn/3, ...)? (Custom Behaviors HOWTO) - callback

That's my suspicion as this simple code
-module(simple_server).
-export( [sayHello/0] ).
-callback say(Num :: term()) -> term().
sayHello() ->
io:fwrite( "Hello 1: ~p\n", [ say(1) ]) ,
io:fwrite( "Hello 2: ~p\n", [ say(2) ]) .
fails to be compiled:
$ erlc simple_server.erl
simple_server.erl:7: function say/1 undefined
simple_server.erl:8: function say/1 undefined
If that is the case, then this is not explicitly commented elsewhere:
official docs, "learn erlang", this answer.

You need to provide the name of the callback module, which can be done through apply and spawn, but you can also make a simple call using a variable as the module name, e.g. CallbackModule:say(1).
So you could do either:
sayHello(CallbackModule) ->
io:fwrite( "Hello 1: ~p\n", [ CallbackModule:say(1) ]) ,
io:fwrite( "Hello 2: ~p\n", [ CallbackModule:say(2) ]) .
or
sayHello(CallbackModule) ->
io:fwrite( "Hello 1: ~p\n", [ apply(CallbackModule, say, [1]) ]) ,
io:fwrite( "Hello 2: ~p\n", [ apply(CallbackModule, say, [2]) ]) .
Those two versions are equivalent.
Let's create a callback module implementing the simple_server behaviour:
-module(my_callback).
-behaviour(simple_server).
-export([say/1]).
say(N) ->
{N, is, the, loneliest, number}.
Now we can call simple_server:sayHello with the module name as the argument:
> simple_server:sayHello(my_callback).
Hello 1: {1,is,the,loneliest,number}
Hello 2: {2,is,the,loneliest,number}

HOWTO Erlang Custom Behaviors (template method pattern).
-1. Write a generic module. Here, an algorithm is defined, but some steps (callback functions in Erlang nomenclature) are left for a future specific definition.
%% generic.erl
-module(generic).
-export( [sayHello/1] ).
-callback say(Num :: term()) -> term(). %% future definition
%% generic algorithm: needs the reference to the provider module
sayHello(ProviderModule) ->
io:fwrite( "Hello 1: ~p\n", [ ProviderModule:say(1) ]) ,
io:fwrite( "Hello 2: ~p\n", [ ProviderModule:say(2) ]) .
-2. Compile generic.erl
erlc generic.erl
-(3.) Try to write a provider (callback) module
%% callbacks1.erl (fails to implement say/1 callback)
-module( callbacks1 ).
-behaviour( generic ).
-(4.) Compile callbacks1.erl: use -pa . to say where generic.beam is. (Therefore, the expected warning about say/1 is issued).
erlc -pa . callbacks1.erl
callbacks1.erl:2: Warning: undefined callback function say/1 (behaviour 'generic')
(Note: If -pa is not given, you'll got this: "callbacks1.erl:2: Warning: behaviour generic undefined")
-3. Write a correct provider (callback) module.
%% callbacks2.erl
-module( callbacks2 ).
-behaviour( generic ).
-export( [say/1] ).
say(1) -> "good morning";
say(2) -> "bon jour";
say(_) -> "hi".
-4. Compile it
erlc -pa . callbacks2.erl
(Ok now).
-5. Write a main.erl to gather generic module with callback module.
%% main.erl
-module( main ).
-export( [main/0] ).
main() ->
%% call the generic algorithm telling it what callback module to use
generic:sayHello( callbacks2 )
. % ()
-6. Compile and run main
erlc main.erl
erl -noshell -s main main -s init stop
We get:
Hello 1: "good morning"
Hello 2: "bon jour"

Related

Coffeescript member is undefined

I get the following error:
Uncaught TypeError: Cannot call method 'push' of undefined
In the next code:
class classDemo
names : ['t1', 't2']
methodM1: () ->
# This works:
#names.push 't3'
console.log #names.toString()
#socket = io.connect()
#socket.on 'connect', () ->
# This raise the error:
#names.push 't4'
console.log #names.toString()
Does anyone know how to push into "names" inside the socket.on method? (How to push 't4' correctly?
Thanks
EDIT: The solution proposed by #Sven works for one level of chaining. It seems to fail for two chained calls. Please consider the following example:
methodM1: () ->
_this = #
#socket = io.connect() # connect with no args does auto-discovery
#socket.on 'connect', () ->
# This works:
_this.names.push 'inside connect'
console.log _this.names.toString()
#socket.emit 'getModels', (data) ->
# This does not work:
_this.names.push 'inside emit'
console.log _this.names.toString()
I tried to apply the same solution again inside connect and before emit (see below) but I get no output:
_this2 = _this
#socket.emit 'getModels', (data) ->
_this2.names.push "inside emit"
console.log _this2.names.toString()
Thanks.
your emit is never fired because emit sends data and requieres therefore a datastructure.
Please change your code like this
a) use the fat arrow
b) emit a data structure
methodM1: ->
#socket = io.connect()
#here use the fat arrow it does the '_this = # automatically'
#socket.on 'connect', =>
#names.push 'inside connect'
console.log _this.names.toString()
#socket.emit 'getModels', yo: "got your message"
The fat arrow always binds to the outer instance (see When does the "fat arrow" (=>) bind to "this" instance)
I am not sure (well, I am pretty sure but havent tried it) that you can send a closure over the wire.

CoffeeScript and Cake Error

I try to get the cake example from http://arcturo.github.io/library/coffeescript/05_compiling.html to run. But that leads to a strange error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
This is my Cakefile (just copied from "Little book on CoffeeSCript")
fs = require 'fs'
{print} = require 'sys'
{spawn} = require 'child_process'
build = (callback) ->
coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
callback?() if code is 0
task 'build', 'Build lib/ from src/', ->
build()
I'm using Coffee 1.6.3 and node 0.10.20.
Does anyone know what I'm doing wrong?
Thanks!
ENOENT typically means "I looked for the thing you told me to find and I didn't find it". From the example page:
For example, create a file called Cakefile, and two directories, lib and src.
Do you have both of those?
I've found an explanation for what is happening here:
Using nodejs's spawn causes "unknown option -- " and "[Error: spawn ENOENT]" errors
The solution was to use exec instead of spawn
On Windows, spawn doesn't handle '.cmd' or '.bat' without file extension.
repalce
coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
with
coffee = spwan 'coffee.cmd', ['-c','-o','lib','src']

BusterJs test in CoffeeScript

I've the follwoing test. For some reason the #$e I've set in the before function is undefined in the test:
assert = buster.assert
buster.testCase 'BaseChart',
before: ->
#el = sinon.spy()
#$el = [#el]
console.log(#$el)
##[LOG] [function spy() {}]
'updates when the model changed': ->
console.log(#$el)
##[LOG] undefined
This is a wild guess but change:
before: -> to before: =>
And see if that helps.

coffeescript autogenerated namespace causes reference error jasmine test

I try to write some simple tests in Coffeescript and Jasmine.
# greet.coffee
greet = (message, person) ->
"#{message}, #{person}!"
and here my Jasmine Spec File:
# greetSpec.coffee
describe 'greet', ->
it 'should greet with message and name', ->
result = greet 'Hello', 'John'
expect(result).toBe 'Hello, John!'
When i start SpecRunner in Jasmine i get:
ReferenceError: greet is not defined
I guess it has something to do with the namespace autogenerated by coffeescript and therefor is the greet function not visible from the Spec file. How can i solve it?
Ok, could solve it with a simple global variable (not sure if that's a good way, though):
greet.coffee:
#greet = (message, person) ->
"#{message}, #{person}!"
greetSpec.coffee:
describe 'greet', ->
it 'should greet with message and name', ->
result = greet 'Hello', 'John'
expect(result).toBe 'Hello, John!'

Node.js REPL eval

I am trying to implement an evaluation into a Node.JS REPL. I am getting the error TypeError: Property 'eval' of object #<REPLServer> is not a function. Here is my source code:
repl.start("> ", socket, socket, true, function(cmd, context, filename, callback){
etc...
repl.start() takes an object with those options, not individual arguments:
e.g. (note the curly brackets)
repl.start({
prompt: "node via stdin> ",
input: process.stdin,
output: process.stdout
});
see the example at: http://nodejs.org/docs/v0.8.4/api/repl.html#repl_repl_start_options