I found some code of Vala, and It works fine.
but when I translate it to genie, it failed.
So, my question is what's the equivalent code of Genie
int get_length<T> (T val) {
if (typeof(T) == typeof(string) ) {
return ((string)val).length;
} else {
GLib.error("Unable to handle type `%s'", typeof(T).name());
}
}
public static void main() {
var myString = "hello";
stdout.printf("%i\n", get_length<string>(myString));
}
my code: genie
def get_length of T (val: T): int
if typeof(T) == typeof(string)
return ((string)val).length
else
pass
init
var s = "hello";
stdout.printf("%i", get_length of string (s))
The error message:
main.gs:2.16-2.17: error: syntax error, expected `(' but got `of' with previous identifier
def get_length of T (val: T): int
^^
update:
the code works.
init
printx of int (123)
printx (456)
printx ("HELLO")
def printx (i: T) of T
case typeof(T)
when 64 // typeof(string)
stdout.printf ("%s\n", (string)i)
when 24 // typeof(int)
stdout.printf ("%i\n", (int)i)
but if I want to have return value
I try
def doubleit (i: T): T of T
and the error messages:
2015-06-29_generic_func.gs:13.27-13.27: error: The type name `T' could not be found
def doubleit (i: T): T of T
^
2015-06-29_generic_func.gs:13.22-13.27: error: The type name `T' could not be found
def doubleit (i: T): T of T
^^^^^^
2015-06-29_generic_func.gs:13.18-13.18: error: The type name `T' could not be found
def doubleit (i: T): T of T
^
Compilation failed: 3 error(s), 0 warning(s)
and try
def doubleit (i: T) of T : T
the error messages:
2015-06-29_generic_func.gs:13.26-13.26: error: syntax error, expected end of line but got `:' with previous identifier
def doubleit (i: T) of T : T
^
Compilation failed: 1 error(s), 0 warning(s)
this code works in Vala:
T doubleit<T> (T i) {
As this doesn't seem possible at the moment I have reported it as a bug.
Related
I am getting error : "A value of type 'num' can't be assigned to a variable of type 'int'."
while executing this code. I got the fix. Just wanted to know why this error. Also Do we need to add "!" for every variable operation?
void main() {
Map <int, int> val = {1:10};
int k = val[1]+5;
//int k = val[1]!+5; //fix
}
o/p dart pad:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:17:
Error: Operator '+' cannot be called on 'int?' because it is potentially null.
int k = val[1]+5;
^
lib/main.dart:3:17:
Error: A value of type 'num' can't be assigned to a variable of type 'int'.
int k = val[1]+5;
^
Error: Compilation failed.
Just put a not null operator (!) in line 3 as shown
void main() {
Map <int, int> val = {1:10};
int k = val[1]!+5;
print(k);
}
no issue was raised during execution, please lmk if the issue is still faced.
I rewrote some my f# functions definitions to static members and stuck at ref/byref parameter error:
static member bar (a : byref<int>) = Foo.bar &a
Error FS0001 This expression was expected to have type
'int ref'
but here has type
'byref<'a>'
Are there some differences with byref parameters between 'let' and static definitions?
UPD:
It's worked example of what I have changed to static member definition:
> let rec foo (a :byref<int>) =
a <- a-1
if a > 0 then
System.Console.Write(a.ToString()); foo &a
else a
;;
val foo : a:byref<int> -> int
> let mutable a = 3;;
val mutable a : int = 3
> foo &a;;
21val it : int = 0
Now it looks like that and it doesn't work. Why? :
> type Foo() =
static member bar (a : byref<int>) =
a <- a-1
if a > 0 then
System.Console.Write(a.ToString()); foo &a
else a;;
type Foo =
class
new : unit -> Foo
static member bar : a:byref<int> -> int
end
> let mutable b = 3;;
val mutable b : int = 3
> Foo.bar &b;;
Foo.bar &b;;
--------^^
stdin(71,9): error FS0001: This expression was expected to have type
'int ref'
but here has type
'byref<'a>'
int ref is type definition of F# Reference Cell, while byref<'a> is an equivalent of C# ref/out arguments (an argument passed by reference even when it's a value type).
While it's impossible to tell anything more without reproducible snippet, it's quite possible that you've conflated two types:
let myFunc(a: int ref) = a := 1
// this is wrong
let a = 1
myFunc &a
// this is right
let a = ref 1
myFunc a
When optional named parameter is not provided, why it is not null as expected?
void main() {
num double({v: num}) {
if (v == null)
return 0;
else
return v * 2;
}
print(double(v: 2));
print(double());
print('done');
}
which output as
4
Uncaught TypeError: v.$mul is not a function
num double({v: num}) {
defines a named parameter v of type dynamic with the default value num (a type)
It should instead be
num double({num v}) {
to make your code work as expected
I was trying out Swift new operator ~= but ran into a weird error.
var filteredNumbers1 = [20,30,50,15].filter({15...30 ~= $0})
Works as expected.
var filteredNumbers2 = [20,30,50,15].filter({$0 ~= 15...30})
Throws error
Binary operator '~=' cannot be applied to operands of type 'int' and 'Range < Int >'
I am wondering what is the difference between 15...30 ~= $0 and $0 ~= 15...30 ?
~= in the Swift standard library is defined to expect the pattern on the left and the value on the right. The declaration is:
public func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool
You could add your own version which supports value on the left and range on the right if you like with something like:
func ~=<I : ForwardIndexType where I : Comparable>(value: I, pattern: Range<I>) -> Bool {
return pattern ~= value
}
Let's take a type augmentation abusive F# script below:
type AugmentMe = val _i : int
type AugmentMe with member i.I = i._i
type AugmentMe with new(i) = { _i = i }
printfn "AugmentMe instance: %i" (AugmentMe(42).I)
that, when being interpreted as a whole, yields the expected Fsi output:
>
AugmentMe instance: 42
type AugmentMe =
class
new : i:int -> AugmentMe
val _i: int
member I : int
end
val it : unit = ()
Now, if we reset Fsi session and interpret the same script, but this time line-by-line, then Fsi fails to add the type constructor with the following diagnostics for the last two script lines:
>
type AugmentMe =
class
val _i: int
end
>
type AugmentMe with
member I : int
>
~vs7894.fsx(3,30): warning FS0073: internal error: pop on empty stack during code generation, methodName = AugmentMe..ctor, m = C:\Users\gene\AppData\Local\Temp\~vs7894.fsx(3,29)-(3,39)
type AugmentMe with
new : i:int -> AugmentMe
>
~vs7894.fsx(4,35): error FS0039: The value or constructor 'AugmentMe' is not defined
After some googling the following mentioning circa 2011 has popped up, but apparently the artifact still has place.
What gives? Should I report it to fsbugs?
Thank you.