What is the equivalent of this kotlin code in dart? - kotlin

In kotlin I was able to create a method like this.
fun add(a:int, b:int, output: (int) -> Unit {
val sum = a + b
output.invoke(sum)
}
Then I could call this method like this:
add(4,5){ sum ->
print(sum) //9
}
Is this possible in dart?
I have looked at a lot of sources but havn't been able to find any help.
Any help is greatly appreciated.

That Kotlin syntax looks fishy, there must be missing brackets. I don't know Kotlin, but I think you want this:
void add(int a, int b, Function(int) output) {
final sum = a + b;
output.call(sum);
}
void main() {
add(4,5, (sum) => print(sum));
}

Turns out it's pretty simple. Silly me.
void add(int a, int b, void c(int)) {
return c.call(5);
}

Related

I'm not understanding this Kotlin example

I'm learning Kotlin at school and it is beig quite difficult to me. I was given this example of code but I'm not understanding at all what it does, specially the part marked as a comment. I hope you can help me. Thanks
open class W{
open fun f(){
println("en W.f()")
}
}
class X : W(){
override fun f(){
println("en X.f")
}
// FROM HERE
inner class Y{
fun g() {
super#X.f()
f()
// UNTIL HERE
}
}
}
fun main() {
val x = X()
x.Y().g()
}
There's a class inside of X called Y. If you look in main, you are creating an X, and then creating a Y through the dot access, and then running the g that is a member of Y.

about CppWinRT internal 2

We know that the implementation class does not inherit from winrt::Windows::Foundation::IUnknow, for example
struct App : ApplicationT<App>
{...}
App does not have the as and try_as member functions, so how does the following code work?
// base.h (v2.0.190620.2 line 509)
template <typename D, typename I>
struct require_one : consume_t<D, I>
{
operator I() const noexcept
{
return static_cast<D const*>(this)->template try_as<I>(); // A
}
};
for the code at A, D is in fact App, it should not work. Please help, many thanks!!!

What is the difference between these ways for defining functions?

I am from a background of Javascript trying to learn some Kotlin.
I know i can define my function by
fun add(a: Int , b: Int): Int{
return a+b
}
I am trying this
val add = {
a:Int,b:Int->
println("I am calculating the sale => no body you guy [$x+$y]");
//works
}
val add = { a:Int ,b : Int ->
//How do i return from this function
}
Also Is this a right way to define Kotlin functions? and Whats the difference with the first way ?
Also Is this a right way to define Kotlin functions? and Whats the difference with the first way ?
This is not even "a way to define Kotlin functions".
In JavaScript, all functions are reified: they are first-class values you can refer to from variables and pass around. Not so in Kotlin, just as in many other languages like Java, C++, Objective C and so on.
A function is just a declaration, you can call it but you can't otherwise directly refer to it. Separate language features allow you to create functional objects that delegate to these functions, and you can pass these objects around.
Therefore,
fun add(a: Int , b: Int): Int {
return a + b
}
is a function declaration and
val add = {a: Int, b: Int ->
a + b
}
is four things:
declaration of a variable add
declaration of an anonymous implementation of the functional type (Int, Int) -> Int
instantiation of this anonymous type, resulting in a functional object
assignment of the object to the variable add.
The object has a method invoke(a: Int, b: Int): Int whose implementation you have given in the block:
fun invoke(a: Int, b: Int): Int {
return a + b
}
You can call it explicitly:
val result = add.invoke(a, b)
and on top of that Kotlin defines syntax sugar that allows you to omit the explicit .invoke.
You don't need the explicit return there
val add = { a: Int, b: Int ->
a + b
}
add(2, 3) // => 5
Hopefully this will work.
val onChange = {
a:Int,b:Int->
println("I am calculating the sale => no body you guy [$x+$y]");
//works
}
val add = { a:Int ,b : Int ->
println("Sunm ${a+b}")
//How do i return from this function
}
Log.v("Response", add(4,3))
Output
V/Response: Sum 7
You can't return values in kotlin like this, it will give error of type mismatch as you havn't declared any return type :
fun add(a: Int , b: Int){
return a+b
} //wrong
we declare return type in kotlin as :
fun add(a: Int , b: Int) : Int{
return a+b
}
Secondly,
val add = { a:Int ,b : Int ->
}
this is not a function, its a declaration of value assignment
In kotlin we declare function by adding "fun" before your function name as
//you can add access modifiers(private,public,protected) if needed just before "fun"(by default its public)
fun add (){ //if it returns any value then add ": {datatype}" just right of "()"
//your code here
}
Hope it helped you :)

The "Builder-style usage of methods that return Unit" on Kotlin website confuse me

The Idioms section of the official Kotlin docs contains this example:
Builder-style usage of methods that return Unit
fun arrayOfMinusOnes(size: Int): IntArray {
return IntArray(size).apply { fill(-1) }
}
As the function apply returns the generic type, and I thought Unit is as same as void in Java, this section is suggesting we can use a void method in builder-style? That doesn't make sense to me - what's it trying to say?
The point it's trying to make is that if you just did traditional Java builder style, like this:
return IntArray(size)
.fill(-1)
then it wouldn't compile, because it's of type Unit, not IntArray.
So traditionally, you'd have to do something like this:
val ret = IntArray(size)
ret.fill(-1)
return ret
apply enables you to avoid this, because the return type is still of type IntArray (or T, in general).
Take this one:
class X {
var a: Int? = null
var b: Int? = null
fun first(a: Int) = apply { this.a = a }
fun second(b: Int) = apply { this.b = b }
}
X().first(2).second(3)
The apply functions are used to return the instance of X after setting the property. This enables builder-style call of both methods. If apply were removed, the function would return Unit.

Dart coding styles

Maybe this is not a Dart specific question.
I have :
class A {
int a;
A(this.a);
}
class B extends A {
String b;
B(a, this.b) : super(a);
}
So you see, class A has an attribute int a and B just extends A and has an extra attribute String b and a wrapper class C :
class C {
A c;
C(this.c);
void doSomething() {
if (c is B) {
print(c.b);
} else {
print(c.a);
}
}
}
The Dart Editor complaints that the c doesn't have a getter b. How do you deal with this? I want to get rid the warning, but I don't want to add attribute b to class A
(c as B) works, but will do a dynamic type check (which will probably be eliminated by the VM).
If you just want to remove the warning, without changing the semantics of the program you can assign to a temporary variable of the correct type:
void doSomething() {
if (c is B) {
B b = c;
print(b.b);
} else {
print(c.a);
}
}
The analyzer shouldn't add a warning here. I think it's related to this issue : Analyzer fails to respect certain "is" checks.
In the meanwhile, you can use (c as B).b to cast c in B.
You can cast expression.
print((c as B).b);