Whats the difference between void and Void in AS3? - actionscript-3

I noticed that i can set a return type on a function to 'Void' aswell as 'void' and just wondered if there was and benefit of either?

Void (with uppercase "v") was ActionScript 2 version of ActionScript 3 void.
AS3 docs (void):
Specifies that a function cannot return any value. The void type is a special type that contains exactly one value: undefined. It is special in that its use is limited to the return type of a function. You cannot use void as a type annotation for a property.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/specialTypes.html#void
AS2 docs (Void):
The Void data type has one value, void, and is used in a function definition to indicate that the function does not return a value, as shown in the following example:
//Creates a function with a return type Void
function displayFromURL(url:String):Void {}
http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00000037.html

No there isn't. void type just says the compiler that no value will be returned.

void type indicates to the compiler that the function you have written will not return any value, in the other side if you indicate other type int than void the compiler expect that you return int.
Ex:
function foo(a:int):int
{
// here the compiler expect that somewhere
// in your function you return an int
return a;
}
AS2 = :Void
AS3 = :void

Related

Dart function in a function

My Code:
void main() {
String name() {
return "Uday Kiran";
}
void printF(var value) {
print(value);
}
printF(name);
}
Hey, I'm sorry if my question is silly. But I'm probably new to the programming and I stuck by doing something like this. I actually wanted to pass the string returned by the name function, as a argument to my printF function, so that the string "Uday Kiran" would be printed on my console. But the output I get is something like this:
Any help would be appreciable:)
The problem is that you are refer to name as a variable which points to a Function instead of calling the function it points to. In Dart, you call a function with () (in case of no arguments) so your example should instead be:
void main() {
String name() {
return "Uday Kiran";
}
void printF(String value) {
print(value);
}
printF(name());
}
I also changed the type of argument to specify you want to get a String as argument as stated in your question. By doing so, you would also got an error explaining the problem with name in your example.

How to get thrust::pair first and second use javacpp

From https://github.com/bytedeco/javacpp/wiki/Interface-Thrust-and-CUDA, I write thrust java code. Struct field is public by default, but I use first() method to get the first field value of thrust::pair, error happen.
error: expression preceding parentheses of apparent call must have (pointer-to-) function type
error: macro "offsetof" passed 3 arguments, but takes just 2
{ sizeof(thrust::pair), offsetof(thrust::pair, first) },
So how to get thrust::pair first and second value ?
#Platform(include="<thrust/pair.h>")
#Namespace("thrust")
public class Thrust {
static { Loader.load(); }
#Name("pair<int, double>")
public static class Pair extends Pointer {
static { Loader.load(); }
public Pair(IntPointer key, DoublePointer value){
allocate(key, value);
}
private native void allocate(#ByRef IntPointer k, #ByRef DoublePointer v);
// will happen error: expression preceding parentheses of apparent call must have (pointer-to-) function type
public native IntPointer first();
// will happen error: macro "offsetof" passed 3 arguments, but takes just 2
{ sizeof(thrust::pair<int, double>), offsetof(thrust::pair<int, double>, first) },
public native Pair first(IntPointer p);
}
public static void main(String[] args) {
IntPointer i = new IntPointer(1);
i.put(10);
DoublePointer d = new DoublePointer(1);
d.put(10.0);
Pair p = new Pair(i, d);
System.out.println(p.first());
}
}

Static call method in Dart Classes (make Classes callable)

For an embedded DSL I want classes to behave like a function. It seems easy for instances (https://www.dartlang.org/articles/emulating-functions/) but I couldn't make it happen for classes. I tried creating a static call a method but this didn't work either.
Is there a way or do I have to give the class another name and make Pconst a function, calling the constructor?
class Pconst {
final value;
Pconst(this.value);
static Pconst call(value) => new Pconst(value);
String toString() => "Pconst($value)";
}
void main() {
var test = Pconst(10);
// Breaking on exception: Class '_Type' has no instance method 'call'.
print("Hello, $test");
}
class TestA {
call(int a, int b) => a + b;
}
void main()
var TA = new TestA();
int integer = TA(3, 4);
print (integer);
}
The call() method is special, in that anyone who defines a call() method is presumed to dynamically emulate a function. This allows us to use instances of TestA as if they were functions that take two integer arguments.
I'd try something like this:
class _PConst{
final value;
_Pconst(this.value);
String toString() => "Pconst($value)";
}
PConst(value){
return new _PConst(value);
}
void main() {
var test = Pconst(10);
print("Hello, $test"); //should work ok
}
so your basically just hiding/wrapping your classes constructor behind a bog standard function.

in defining a function in Dart, how to set an argument's default to { }, ie, an empty Map?

love how Dart treats function arguments, but cannot accomplish what should be a simple task:
void func( String arg1, [ Map args = {} ] ) {
...
}
get the error
expression is not a valid compile-time constant
have tried new Map() for example, with same error.
You have to use the const keyword :
void func( String arg1, [ Map args = const {} ] ) {
...
}
Warning : if you try to modify the default args you will get :
Unsupported operation: Cannot set value in unmodifiable Map
The default value must be a compile time constant, so 'const {}' will keep the compiler happy, but possibly not your function.
If you want a new modifiable map for each call, you can't use a default value on the function parameter. That same value is used for every call to the function, so you can't get a new value for each call that way.
To create a new object each time the function is called, you have to do it in the function itself. The typical way is:
void func(String arg1, [Map args]) {
if (args == null) args = {};
...
}

"Functions" versus "function pointers" -- what's the difference?

I'm really confused at the documentation of isSomeFunction, as well as at the following code:
static assert(!isFunctionPointer!(typeof(Object.toString))); // makes sense
static assert(!isDelegate!(typeof(Object.toString))); // what??
static assert( isSomeFunction!(typeof(Object.toString))); // what??
Could someone please explain the difference between a "function" and a "function pointer" to me?
Short Answer:
is(T == function)      Whether T is a function
isFunctionPointer!T  Whether T is a function pointer (and not a delegate)
isDelegate!T                Whether T is a delegate
isSomeFunction!T        Whether T is a function, a function pointer, or a delegate
Long Answer:
A function is, well, a function.
auto func(int val) {...}
It's a chunk of code with a name that you can call using that name. You give it arguments, it does whatever it does, and it returns a result. You can call it, but you can't pass it around. You need a function pointer for that.
A function pointer is a pointer to a function. So just like int is an int and int* is a pointer to an int, and int is not a pointer to int, a function isn't a function pointer. If you want a function pointer, you need a pointer to a function. The syntax is different from how it is with int, but it's the same concept.
A delegate is a function pointer with state. So for instance, in
int foo(int value)
{
int bar()
{
return value + 5;
}
auto barDel = &bar;
return barDel();
}
void main()
{
auto fooFunc = &foo;
}
foo is a function, bar is a nested function which has access to its outer scope, and barDel is a delegate, because it's a function pointer with state (the outer state that bar has access to). If you pass barDel to another function (or return it), you'll get a closure (unless the function it's passed to takes the delegate by scope, in which case that function guarantees that the delegate will not escape its scope), because that state needs to be put on the heap so that it continues to exist even if the function call that its state comes from has completed when it's called. funcFoo, on the other hand, is a function pointer, because foo doesn't have any outer state. If bar were static, then barDel would also be a function pointer rather than a delegate, because bar would no longer have access to the function that it's in (though its body would then have to be changed, since it would no longer have access to value).
Now, as to your example. Object.toString is a member function of Object. So, it's a function. It has no state associated with it. Functions never do. Its current signature is
string toString();
But because it's a member function of Object, its signature is really something like
string toString(Object this);
this is passed to toString as an argument. It's not state associated with toString. So, &Object.toString is not a delegate. It's just a function pointer. And Object.toString isn't a function pointer, so even if &Object.toString were a delegate, static assert(isDelegate!(typeof(Object.toString))) would still fail, because in order to be a delegate, it must be a function pointer, which it's not. It's a function.
Now, unfortunately, typeof(&Object.toString) is considered to be string function() rather than string function(Object), so using it to call toString with an actual Object takes a bit of work. It can be done, but I don't remember how at the moment (and it's a bit ugly IIRC). But it wouldn't be a delegate regardless, because there's no state associated with it.
If you want a function that you can pass an Object to and have it call a member function, then you could do something like
auto obj = getObjectFromSomewhere();
auto func = function(Object obj){return obj.toString();};
auto result = func(obj);
If you want to associate an object with a member function and be able to call that member function on that object without having to pass the object around, then you just wrap it in a delegate:
auto obj = getObjectFromSomewhere();
auto del = delegate(){return obj.toString();};
auto result = del();
This bit of code should sum things up and illustrate things fairly well:
int foo(int value)
{
int bar()
{
return value + 5;
}
static assert( is(typeof(bar) == function));
static assert(!isFunctionPointer!(typeof(bar)));
static assert(!isDelegate!(typeof(bar)));
static assert( isSomeFunction!(typeof(bar)));
auto barDel = &bar;
static assert(!is(typeof(barDel) == function));
static assert(!isFunctionPointer!(typeof(barDel)));
static assert( isDelegate!(typeof(barDel)));
static assert( isSomeFunction!(typeof(barDel)));
static int boz(int i)
{
return i + 2;
}
static assert( is(typeof(boz) == function));
static assert(!isFunctionPointer!(typeof(boz)));
static assert(!isDelegate!(typeof(boz)));
static assert(isSomeFunction!(typeof(boz)));
auto bozFunc = &boz;
static assert(!is(typeof(bozFunc) == function));
static assert( isFunctionPointer!(typeof(bozFunc)));
static assert(!isDelegate!(typeof(bozFunc)));
static assert( isSomeFunction!(typeof(bozFunc)));
return boz(bar());
}
static assert( is(typeof(foo) == function));
static assert(!isFunctionPointer!(typeof(foo)));
static assert(!isDelegate!(typeof(foo)));
static assert( isSomeFunction!(typeof(foo)));
void main()
{
auto fooFunc = &foo;
static assert(!is(typeof(fooFunc) == function));
static assert( isFunctionPointer!(typeof(fooFunc)));
static assert(!isDelegate!(typeof(fooFunc)));
static assert( isSomeFunction!(typeof(fooFunc)));
}
static assert( is(typeof(Object.toString) == function));
static assert(!isFunctionPointer!(typeof(Object.toString)));
static assert(!isDelegate!(typeof(Object.toString)));
static assert( isSomeFunction!(typeof(Object.toString)));
static assert(!is(typeof(&Object.toString) == function));
static assert( isFunctionPointer!(typeof(&Object.toString)));
static assert(!isDelegate!(typeof(&Object.toString)));
static assert( isSomeFunction!(typeof(&Object.toString)));
isSomeFunction is true for all of them, because they're all either functions, function pointers without state, or delegates.
foo, bar, boz, and Object.toString are all functions, so they're true for is(T == function) but not for the others.
fooFunc, bozFunc, and &Object.toString are function pointers without state, so they're true for isFunctionPointer!T but not for the others.
barDel is a delegate, so it's true for isDelegate!T but not for the others.
Hopefully, that clears things up for you.