Function<String, int> in Dart? - function

I have a function with a callback void doSth(Function callback), however I would like to specify the parameters and return value of the callback like in Java.
Is this possible in Dart?
(I can't use Future here)

void doSth(String Function callback(int /* type for parameter */ ))
See also https://www.dartlang.org/guides/language/effective-dart/design#prefer-inline-function-types-over-typedefs

Related

How can I create a function which accepts a function with specific parameters as an argument?

I'd like to create a function which accepts a function that accepts specific types of parameters as an argument. For example:
myFn(Function paramFn) {
paramFn([1, 2, 3]);
}
How can I ensure that paramFn accepts a List<int> as an only parameter?
You can use typedef to define the signature you want like described in Kul's answer or you can simply inline the function signature in the parameter:
myFn(void paramFn(List<int> l)) {
paramFn([1, 2, 3]);
}
You can use typedef to associate a symbol with a function that satisfies the signature you want. Something like
typedef void ParamFn(List<int> l);
myFn(ParamFn f) {
f('abc'); // compile time error
f([1,2,3]); // works fine
}
That's what typedefs are for, although I'm not sure how rigid the strong mode will enforce it yet.

How do I cast C++/CX runtime object to native C pointer type?

I am doing a C++/CX runtime wrapper, and I need pass C++/CX Object pointer to native C. How do I do it, and convert the native pointer back to C++/CX Object reference type?
void XClassA::do(XClass ^ B)
{
void * ptr = (void*)(B); // how to convert it?
}
And also, C++/CX uses Reference Counting, if I cast the Object reference to native pointer, how do I manage the pointer life cycle?
update (request from #Hans Passant)
Background of the question,
Native C
I am trying to use C++/CX wrap Native C library (not C++) as Windows Runtime Component. Native c has many callback functions which declared as the following,
for example,
//declare in native c
typedef int (GetData*)(void *, char* arg1, size_t arg2);
void * is a pointer to object instance.
and the callback will be executed in native c during runtime.
We expect Application(C#/C++CX ...) to implement the method.
WinRT wrapper (C++/CX)
my idea is the following,
(1) Provide interface to Application
// XRtWrapperNamespace
public interface class XWinRtDataWrapper
{
//declare in base class
void getData(IVector<byte> ^ data);
}
to let Application implement the function. As I cannot export native data type, I provide IVector to get data from Application.
(2) Declare a global callback function to convert IVector<byte>^ to native data type char *, like following,
// when Native C executes callback function,
// it will forward in the method in C++/CX.
// The method calls the implementation method via object pointer.
// (And here is my my question)
void XRtWrapperNamespace::callbackWrapper(void * ptr, char *, int length)
{
// create Vector to save "out" data
auto data = ref new Vector<byte>();
// I expect I could call the implementation from Application.
ptr->getData(data); // bad example.
// convert IVector data to char *
// ...
}
My question is
How do I keep windows object reference to native C?
It looks impossible, but any solution to do it?
Application (example)
//Application
public ref class XAppData: public XWinRtDataWrapper
{
public:
virtual void getData(IVector<byte> ^ data)
{
//implementation here
}
}
You are not on the right track. I'll assume you #include a c header in your component:
extern "C" {
#include "native.h"
}
And this header contains:
typedef int (* GetData)(void* buffer, int buflen);
void initialize(GetData callback);
Where the initialize() function must be called to initialize the C code, setting the callback function pointer. And that you want the client code to directly write into buffer whose allocated size is buflen. Some sort of error indication would be useful, as well as allowing the client code to specify how many bytes it actually wrote into the buffer. Thus the int return value.
The equivalent of function pointers in WinRT are delegates. So you'll want to declare one that matches your C function pointer in functionality. In your .cpp file write:
using namespace Platform;
namespace YourNamespace {
public delegate int GetDataDelegate(WriteOnlyArray<byte>^ buffer);
// More here...
}
There are two basic ways to let the client code use the delegate. You can add a method that lets the client set the delegate, equivalent to way initialize() works. Or you can raise an event, the more WinRT-centric way. I'll use an event. Note that instancing is an issue, their is no decent mapping from having multiple component objects to a single C function pointer. I'll gloss this over by declaring the event static. Writing the ref class declaration:
public ref class MyComponent sealed
{
public:
MyComponent();
static event GetDataDelegate^ GetData;
private:
static int GetDataImpl(void* buffer, int buflen);
};
The class constructor needs to initialize the C code:
MyComponent::MyComponent() {
initialize(GetDataImpl);
}
And we need the little adapter method that makes the C callback raise the event so the client code can fill the buffer:
int MyComponent::GetDataImpl(void* buffer, int buflen) {
return GetData(ArrayReference<byte>((byte*)buffer, buflen));
}

Where would noop be used in Ceylon

I am playing around with this beautiful language and saw a function called noop.
As the documentation says it's a void function that does nothing!!
So why would I use a function that does nothing? Is it for adding "Nop" in assembly (for pipeline etc) but then this would be too low-level wouldn't it?
noop() can take the place of any void (or Anything returning) function. So it's useful to use as a value if you are calling a function or creating an object that requires you to pass in an event handler or callback function, but you aren't interested in responding to the event.
noop() is also useful as the default value of an an optional parameter of a function, for example:
void foo(void bar(Integer i) => noop(i)) {}
Or:
void foo(Anything(Integer) bar = noop) {}

Non-void function used in void context?

I am using SystemVerilog. My code is:
function write_pixel_data(datastr ds);
/* some stuff here... but no return */
endfunction
then i am calling my function like:
write_pixel_data(someval);
And i get the vcs warning:
Warning-[SV-NFIVC] Non-void function used in void context.
But i am not returning anything, i know i can cast the function call to void to get rid of the warning. But why it gives this warning??!!
Thanks.
If you haven't declared the function as void and you call it without assigning the return value to anything, you'll see this error. Simple fix:
function void write_pixel_data(datastr ds);
/* some stuff here... but no return */
endfunction
Careful though, you can't do anything that 'takes time' in a function. You'll need a task for that.
A function declared with an implicit type returns logic. You must explicitly declare the return type to be void if that is your intention.

what is the point of void in AS3

Simple question here, when void follows a function in AS3 what is it doing?
public function sayGoodbye():void { trace("Goodbye from MySubClass");}
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 T than void the compiler expect that you return T.
Ex:
function foo(a:int):int { // here the compiler expect that somewhere
// in your function you return an int
return a;
}
void means that it has no return value. I.e., you can't use it in an expression.
void specifies that the function will return no value, or, to be more exact, the special undefined value type. Note that the function return can be used in an expression and it is the unique value of the undefined type.
In actionscript 3 to conform to the strict mode you need to specify variable types and function return types in order for the compiler to know what types to expect and to optimize your application.