Method Overloading

59 views
Skip to first unread message

Mohammad Als

unread,
Sep 7, 2018, 10:05:22 AM9/7/18
to ceylon-users


I'm aware that Ceylon does not allow method overloading. But it is achievable using java.lang {overloaded } annotation.

import java.lang {
ObjectArray,
overloaded
}
shared abstract class AbstractOp<T>() of T given T satisfies AbstractOp<T> {
     /**
     * SQL FROM clause
* @return Returns this object
* @param table The table to use with FROM clause
*/
shared overloaded default T from(Table table) {
op().append(" FROM ");
op().append(table.getTableName());
return (this) of T;
}

/**
* SQL FROM clause.
* @return Returns this object
* @param tables The tables to use with FROM clause
*/
shared overloaded T from({Table*} tables) {
value len = tables.size;
variable Integer cnt = 1;
op().append(" FROM(");
for(table in tables){
op().append(table.getTableName());
if(cnt++ < len){
op().append(",");
}
}
op().append(")");
return (this) of T;
}
}

I've another class where I'm using overloaded annotation for static methods:
shared class ColumnValue {

shared overloaded static ColumnValue setVal(Column column, Anything val) {
if(!isValid(val)) {
throw Exception("Invalid type passed for table column:``column`` ");
}
return ColumnValue(column,val);
}

shared overloaded static ColumnValue setVal(Anything val) {
if(!isValid(val)) {
throw Exception("Invalid value passed.");
}
return ColumnValue(Column(""),val);
}

Column column;
Anything val;

new(Column column, Anything val){
this.column = column;
this.val = val;
}

shared Anything getValue() => val;

shared Column getColumn() => column;
}

Now, I'm trying to import the method "setVal" statically but it fails..."Illegal static import, setVal is not static"

However, it allows to use setVal if:

1) I call it using ColumnValue.setVal("Germany");
2) It allows static import if I remove the overloaded annotation and name the two methods differently as below:

shared class ColumnValue {

shared static ColumnValue set(Column column, Anything val) {
if(!isValid(val)) {
throw Exception("Invalid type passed for table column:``column`` ");
}
return ColumnValue(column,val);
}

shared static ColumnValue setVal(Anything val) {
if(!isValid(val)) {
throw Exception("Invalid value passed.");
}
return ColumnValue(Column(""),val);
}

Column column;
Anything val;

new(Column column, Anything val){
this.column = column;
this.val = val;
}

shared Anything getValue() => val;

shared Column getColumn() => column;
}


Gavin King

unread,
Sep 10, 2018, 7:40:29 AM9/10/18
to ceylon...@googlegroups.com
Hi, I just tried the following code, which compiled and executed
perfectly on but 1.3.3 and current git:


import java.lang {
overloaded
}

shared void run() {
Ceylon.foo("", 2);
Ceylon.foo(10);
Ceylon.foo(1.2);
Ceylon.foo("", 1.2);
}


class Ceylon {
shared static overloaded void foo(String s, Integer i) {
print("string, int");
}
shared static overloaded void foo(Integer i) {
print("int");
}
shared static overloaded void foo(Anything any) {
print("any");
}
shared static overloaded void foo(String s, Anything any) {
print("string, any");
}
shared new () {}
}

I really can't see what's different about your code.
> --
> You received this message because you are subscribed to the Google Groups "ceylon-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-users...@googlegroups.com.
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/9e85faf7-3a9d-41dd-b4c8-30a761e9bf93%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gavin King
Red Hat
ga...@ceylon-lang.org
@1ovthafew

Mohammad Als

unread,
Sep 10, 2018, 10:05:37 AM9/10/18
to ceylon-users
I want to know if foo() can be statically imported..?

import {
  Ceylon{
     foo
  }
}

Is this possible?

Gavin King

unread,
Sep 10, 2018, 10:26:24 AM9/10/18
to ceylon...@googlegroups.com
Oh, now I understand. OK, let me take a closer look.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ceylon-users/d0b94f2c-8657-48e1-bee6-572c8fc10f77%40googlegroups.com.

Gavin King

unread,
Sep 10, 2018, 11:03:45 AM9/10/18
to ceylon...@googlegroups.com

Mohammad Als

unread,
Sep 10, 2018, 11:41:04 AM9/10/18
to ceylon-users
Thank you :)

Gavin King

unread,
Sep 10, 2018, 12:53:10 PM9/10/18
to ceylon...@googlegroups.com
Welcome.

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages