After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 752365 - Genie does not support declaring generic functions with return values
Genie does not support declaring generic functions with return values
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Genie
unspecified
Other All
: Normal normal
: ---
Assigned To: Jamie McCracken
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2015-07-14 10:29 UTC by Jens Mühlenhoff
Modified: 2018-05-22 15:25 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Annotated example of how interfaces can be used (3.44 KB, text/plain)
2016-01-11 17:32 UTC, Al Thomas
Details

Description Jens Mühlenhoff 2015-07-14 10:29:05 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)
Comment 1 Al Thomas 2016-01-11 17:32:49 UTC
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.
Comment 2 Al Thomas 2016-01-11 18:39:57 UTC
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"
Comment 3 GNOME Infrastructure Team 2018-05-22 15:25:37 UTC
-- 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.