GNOME Bugzilla – Bug 752365
Genie does not support declaring generic functions with return values
Last modified: 2018-05-22 15:25:37 UTC
From this stackoverflow question: http://stackoverflow.com/questions/31097190/what-is-the-syntax-for-generic-methods-in-genie [indent=4] 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 is: generic_function.gs:3.16-3.17: error: syntax error, expected `(' but got `of' with previous identifier def get_length of T (val: T): int ^^ Compilation failed: 1 error(s), 0 warning(s)
Created attachment 318807 [details] Annotated example of how interfaces can be used This example hopefully starts to document some more details of the Genie type system. The example includes using an interface as a way of implementing a parameter of type. It also shows how parameters of type can be used with an interface and how to write classes that implement multiple interfaces that include parameters of type.
This bug title, "Genie does not support declaring generic functions", is a bit too wide. Genie does support generic functions: [indent = 4] init test( new TestOne() ) test( new TestTwo() ) def test( a:T ) of T if a isa TestOne print "TestOne" if a isa TestTwo print "TestTwo" class TestOne val:string = "example" class TestTwo val:string = "example" The above compiles and works. The bug title should be something like "Genie does not support declaring generic functions with return values". The following fails: [indent = 4] init print test( new TestOne() ) print test( new TestTwo() ) def test( a:T ) of T:string if a isa TestOne return "TestOne" if a isa TestTwo return "TestTwo" return "unknown" class TestOne val:string = "example" class TestTwo val:string = "example" with the message "error: syntax error, expected end of line but got `:' with previous identifier". Replacing "of T" with "of (T)" results in "error: syntax error, expected identifier"
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/507.