Cannot convert function to delegate - function

I have this function
private ulong Html(ubyte[] data)
{
return data.length;
}
that I want to convert to delegate by using toDelegate() function. I have tried it:
client.onReceive = toDelegate(&Html);
But I'm getting an error message:
lixo.d(722): Error: not a property client.onReceive
/home/$/D/libs/arsd/dom.d(743): Warning: statement is not reachable
/usr/include/d/dmd/phobos/std/range.d(595): Error: static assert "Cannot put a dchar[] into a Appender!(char[])"
/usr/include/d/dmd/phobos/std/format.d(1758): instantiated from here: put!(Appender!(char[]),dchar[])
/usr/include/d/dmd/phobos/std/format.d(1514): instantiated from here: formatRange!(Appender!(char[]),dchar[],char)
/usr/include/d/dmd/phobos/std/conv.d(101): instantiated from here: formatValue!(Appender!(char[]),dchar[],char)
/usr/include/d/dmd/phobos/std/conv.d(757): ... (1 instantiations, -v to show) ...
/usr/include/d/dmd/phobos/std/conv.d(244): instantiated from here: toImpl!(char[],dchar[])
/home/$/libs/arsd/dom.d(2115): instantiated from here: to!(dchar[])
How to fix this?

I guess the /home/$/D/libs/arsd/dom.d library has a struct or class called 'Html'. Try renaming your 'Html' function to something else and see if it helps.
If it still doesn't work you probably have to show us more code as your example seems to work fine for me:
http://dpaste.dzfl.pl/fd729f3d
(I've seen similar errors before. For some reason the "not a property" error occurs a lot since dmd 2.060 even if the real error is not related.)

Related

AS3: Not getting typeError when calling a function

I'm writing my own language from ActionScript as a personal project (yeah, I guess AS3 is not the best language to build a language from, but never mind that).
NOTE: I have checked several times, and my compiler's option 'Enable Strict Mode' is set to True. I have tried setting it to False to try, but I didnt get a different result.
At any rate, I have a this:
package NodyCode.Classes
{
public class NCString
{
var value:String;
public function NCString(expression:String = "") {
value = expression;
}
public function rindex(substr:NCString, startIndex:int = 0x7fffffff):uint {
//code here
}
}
}
Since I'm writing my own language, I need to make sure functions and methods can take un unlimited number of arguments. For this reason, I'm using an anonymous function so that I can use the apply method. Like so:
//This code is in a class named ClassMethods
public static var StringMethods:Object = {
rindex: function(substr:NCString, startIndex:int = 0x7fffffff):uint {
return this.rindex(substr, startIndex);
}
}
And, somewhere else in my code, I do the call:
return ClassMethods.StringMethods["rindex"].apply(ncstr1, [ncstr2, [5]]);
I would like an error to be thrown whenever the user uses the wrong type of argument.
So, in this case, I call the rindex method on ncstr1, with arguments: substr = ncstr2 and startIndex = [5]. Notice that, according to my anonymous function's definition, startIndex is supposed to be an int, not an Array.
So, I expected an error to be thrown. Instead, though, rindex is called with startIndex = 5.
Why is [5] converted to 5, and is there any way for me to prevent that? If there isn't, I can always work around this problem, but I'd rather not if I can do otherwise.
EDIT: Finally understood that I did not mention I was using an anonymous function.
Are you compiling with strict mode set to false? (See here also.)
The strict option: "Prints undefined property and function calls; also performs compile-time type checking on assignments and options supplied to method calls".
It defaults to true, but if it got set to false somehow, compile-time checks might be disabled. I'd check your compiler settings (whether in an IDE or if you're compiling on the command-line) and make sure they're correct.
Okay, so here's what was said in the comments:
I did have my compiler on strict mode. The reason for which I was not getting an error is because I was using the apply method of an anonymous function. The type checks are loosened when using the apply method. That's why [5] was coerced to 5.
There is apparently no way to prevent this.

Why do I get Error #2004 while passing two strings to a function in AS3?

My Function's definition :
public function updateTextField(value:String, label:String):void{
.....
}
I call it as below :
updateTextField("Level : 1 Passed","L1");
And I get below error while debugging it using FDB, and the function's content isn't updated ( Textfield isn't updated ) :
"[Fault] exception, information=ArgumentError: Error #2004: One of the parameters is invalid."
As far as I can understand, my function expects two strings, and that's why I provide it with.
.
I added a try-catch block when I call the function.
try{
updateTextField("Level : 1 Passed","L1");
} catch(e1:ArgumentError) {
trace("1. " + e1);
}
Now , though it throws exception, but before that the function gets executed successfully. Though, it solves my problem up to an extent (except I will have to call the function inside try-catch every time)
But I would like to understand why I get such error when correct arguments are bring passed, and how could I rectify it ?

AS3 - do functionClassA.call() in a functionClassB with parameters

I read this : http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Function.html
But that does not exactly correspond to what I want to do, and I can't find the right solution. However my question is not so complicated.
Here is the situation :
mains.as contains a functionA(strParam:String)
onlineClass.as contains a functionB working like this :
private static functionB (fnParam:Function):void //my fnParam is functionA
{
var strParam:String = getSomeStringResult();
//I have a result from a function
fnParam.call(strParam);
//I want to execute functionA with strParam as parameter
}
But I don't understand what I have to do with call parameters.
I tried :
fnParam.call(null, strParam);
But it returns an error :
[Fault] exception, information=TypeError: Error #1009: Impossible to access a property or a method of a null object's reference
I am sure the answer already exists somewhere but a search with "function" and "call" leads nowhere.
Thank you for the help.
If a parameter is a function then you can call it directly as the parameter name if it's being passed to another function:
functionB(fnParam:Function){
fnParam('strParam');
}
The issue here looks like like the functionA is null when it's being passed through to functionB. This is possibly because they're in different files / classes but you'll probably want to debug before functionB is called to make sure functionA is accessible.
Ok, I made my code working, and I think it could be a static story. FunctionA is not static, and when I pass it as a parameter of the static FunctionB, this works :
private static var functionACallback:Function;
public static functionB( functionA:Function ):void
{
functionACallback = functionA; //set the static functionACallback
var strParam = getSomeStringResult(); //get the string
functionACallback(strParam); //call the static var with parameter
}
I don't entirely understand the issue here, but the above code resolve the problem.
defenestrate.me's answer was helpful.

AS3: Is it possible to give a vector function argument a default value?

I have a function where I'd like to make a vector argument optional-- that is, something like this:
public function test(arg1:int, arg2:Vector.<int> = new Vector.<int>(5)) {}
So in that example, I want the first argument to be required, and an optional vector passed in. If the second argument is not provided, create an int vector with 5 elements instead. It throws a compile error: "Parameter initializer unknown or is not a compile-time constant."
Making the argument not optional works, as in:
public function test(arg1:int, arg2:Vector.<int>) {}
But that's not exactly what I'm looking for. Doing some searching I found a supposed workaround, which is
public function test(arg1:int, arg2:Vector.<int> = null) {}
But that doesn't compile either.
I've already moved on in my code with a workaround just to be done with it, but I'm still curious. Can you have a vector as a default argument, and how?
I don't think this is possible. Probably just because the compiler was never programmed to handle this situation because optional parameters do work with many other datatypes in AS3. I did some research and other have reported the same issue as you with no success in setting an empty vector object in the function declaration.
I would simply do the following if you haven't already:
var myDefaultVector:Vector.<int> = new Vector.<int>(5);
function test(arg1:int, arg2:Vector.<int> = null) {
if( arg2 == null ) {
arg2 = myDefaultVector;
}
// rest of your code
}
I have tried compiling the above code in Flash and it compiled successfully.

AS3 Access the methods of a dynamically created movieclip

How do I access the methods of a dynamically created movieclip/object?
For simplicity sake I didn't post code on how I dynamically created the movieclip. Instead, assume its already created. It is an object. It is called field_2. Below it is referenced by using getChildByName('field_' + field.id);
Check_box_component.as
public var testVar:String = 'test';
public function testReturn()
{
return 'value returned';
}
Main.as
var temp:MovieClip = MovieClip(getChildByName('field_' + field.id));
trace(temp);
trace(temp.testReturn);
trace(temp.testVar);
Output:
[object Check_box_component]
function Function() {}
test
When I trace temp.testReturn, why does it show "function Function() {}" instead of "value returned"?
This link below helped me get this to this point.
http://curtismorley.com/2007/06/13/flash-cs3-flex-2-as3-error-1119/
have you tried:
trace(temp.testReturn());
... instead of your
trace(temp.testReturn);
... ?
I think you will have the result you are waiting for.
Actually, when doing "temp.testReturn", you are not calling the function. You need to add the parenthesis to make the actual call.
When you make a trace of temp.testReturn, the function is not executed: the trace function tell you the type of temp.testReturn, which is here correctly returned as a "function" type.
There is a difference between a function reference and a function call. Parenthesis '()' are an operator sign of ActionScript. They tell the compiler "please try to make a call to what was just behind us". Or at least I hope they are that polite.
A function in ActionScript is an object, like all other stuff. A member of Function class. You can pass it's reference back and forth, you can even call it's methods like call() or apply().
If you want a call, and not a reference, you have to use call operator.
trace(temp.testReturn());
EDIT You accepted an answer while I was typing, sorry for a duplicate answer.