Are delegates supported in some way?

197 views
Skip to first unread message

Bienlein

unread,
Oct 22, 2013, 4:16:15 AM10/22/13
to ceylon...@googlegroups.com
Hello,

is there a way to get something accomplished like delegates as in Kotlin or Go? If not, what about adding delegates to the language ;-) ? IMHO delegates are really useful and save you from having to make the IDE generate methods that do the delegation. All those generated delegation methods only pollute your classes...

Regards, Bienlein

Gavin King

unread,
Oct 22, 2013, 5:26:35 AM10/22/13
to ceylon...@googlegroups.com
No, we don't, and I don't really see how they're more useful than
java.lang.reflect.Proxy (which we probably should provide some kind of
native support for). I mean, I don't precisely know the semantics of
this feature in Kotlin or Go, but isn't it just the same thing?

A data point: in the 100s of thousands of lines of Java code I have
written over the past three years, I don't believe I've written a
single "delegate". And I've certainly never run into a problem where
java.lang.reflect.Proxy would have been helpful. And I don't even have
multiple inheritance in Java! With Ceylon's inheritance model, I would
have even less use for delegates!

I would *much* rather have interception/instrumentation built into the
language as a feature of the metamodel, than "delegates".
Instrumentation is much more widely useful, and a better solution to
the things I've seen people use proxies for in Java.

It almost makes me want to repudiate the cute "decorators"
functionality in CDI...
> --
> 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 http://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/4a7527e4-4be2-426b-b544-33ac7ed70ac4%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Gavin King
ga...@ceylon-lang.org
http://profiles.google.com/gavin.king
http://ceylon-lang.org
http://hibernate.org
http://seamframework.org

Gavin King

unread,
Oct 22, 2013, 5:28:54 AM10/22/13
to ceylon...@googlegroups.com
On Tue, Oct 22, 2013 at 10:16 AM, Bienlein <jet...@web.de> wrote:

> is there a way to get something accomplished like delegates as in Kotlin or
> Go?

Actually, perhaps I should have responded: "yes, java.lang.reflect.Proxy". ;-)

Bienlein

unread,
Oct 22, 2013, 5:38:45 AM10/22/13
to ceylon...@googlegroups.com
Hello,

I think java.lang.reflect.Proxy is a runtime thing where some message is forwarded at runtime through reflection. This is not type safe and not good for performance. In Java I miss mixins more than delegates, true. But delegates are an important aspect in OOP (is-a vs. has-a). Kotlin's stateless traits and its delegates can be combined and in sum provide something like stateful traits without the problems of stateful traits.There is a sample here:

trait Base {
  fun print()
}
 
class BaseImpl(val x : Int) : Base {
  override fun print() { print(x) }
}
 
class Derived(b : Base) : Base by b
 
fun main() {
  val b = BaseImpl(10)
  Derived(b).print() // prints 10
}

I find this idea of combining traits and delegates really elegant and useful.

Regards, Bienlein

Gavin King

unread,
Oct 22, 2013, 5:40:26 AM10/22/13
to ceylon...@googlegroups.com
There, I opened an issue:

https://github.com/ceylon/ceylon.language/issues/333

This pretty much amounts to providing something very similar to CDI's
interceptors/decorators via an API call.

Gavin King

unread,
Oct 22, 2013, 5:41:29 AM10/22/13
to ceylon...@googlegroups.com
On Tue, Oct 22, 2013 at 11:38 AM, Bienlein <jet...@web.de> wrote:

> I think java.lang.reflect.Proxy is a runtime thing where some message is
> forwarded at runtime through reflection. This is not type safe and not good
> for performance.

Depends how you use it. CDI decorators are totally typesafe. CDI
interceptors are detyped. Both styles are useful.

Gavin King

unread,
Oct 22, 2013, 5:42:46 AM10/22/13
to ceylon...@googlegroups.com
On Tue, Oct 22, 2013 at 11:38 AM, Bienlein <jet...@web.de> wrote:

>> trait Base {
>> fun print()
>> }
>>
>> class BaseImpl(val x : Int) : Base {
>> override fun print() { print(x) }
>> }
>>
>> class Derived(b : Base) : Base by b
>>
>> fun main() {
>> val b = BaseImpl(10)
>> Derived(b).print() // prints 10
>> }


I don't understand this code example. What does it do? Looks like
Derived is just a totally trivial empty decorator.

Gavin King

unread,
Oct 22, 2013, 6:00:33 AM10/22/13
to ceylon...@googlegroups.com
In case people don't know what I'm talking about here, what I'm saying
is that we can provide an API that lets you do this:

interface I {
shared formal void x();
shared formal void y();
}

interface D satisfies I {
shared formal delegate I i;
shared actual void x() {
i.x();
}
}

class C() satisfies I {
shared actual void x() {}
shared actual void y() {}
}

I decorated = Decorator(D).decorate(C());

This is essentially just a transliteration of the decorators model in CDI.

AFAICT, I don't need to add any new special syntax to the language to
achieve this.

Bienlein

unread,
Oct 22, 2013, 6:57:09 AM10/22/13
to ceylon...@googlegroups.com
I don't understand this code example. What does it do? Looks like
Derived is just a totally trivial empty decorator.

Derived is a class that uses BaseImpl as a delegate (denoted by the by-clause in "Base by b"). Derived obtains the state of BaseImpl, which is the variable x. Note that BaseImpl is a class, not a trait (although Derived extends the trait Base). A good example of delegates in Go is in the chapter named "Inheritance" in this article.

-- Bienlein

Gavin King

unread,
Oct 22, 2013, 7:34:49 AM10/22/13
to ceylon...@googlegroups.com
But Derived doesn't _do anything_. Does it? What is it for?

Gavin King

unread,
Oct 22, 2013, 7:35:57 AM10/22/13
to ceylon...@googlegroups.com
On Tue, Oct 22, 2013 at 1:34 PM, Gavin King <gavin...@gmail.com> wrote:

> A good example of delegates in Go is in the chapter named "Inheritance" in this article.

I would prefer if _you_ explained to me, in your words, how it differs
from my example with "Decorator(D).decorate(C())".

Bienlein

unread,
Oct 22, 2013, 8:05:07 AM10/22/13
to ceylon...@googlegroups.com


But Derived doesn't _do anything_. Does it? What is it for?

Derived is simply a class that holds on to some class delegated to such as Foo in the sample Groovy code below that uses a @Delegate Bar:

class Bar {
    def bar() {
        println("bar")
    }
}
 
class Foo {

    @Delegate
    def Bar bar = new Bar()

    def static main(String[] args)
    {
        def foo = new Foo()
        foo.bar()
    }
}

Prints "bar". @Delegate in Groovy is type-safe and no wiring is required.

Gavin King

unread,
Oct 22, 2013, 12:01:48 PM10/22/13
to ceylon...@googlegroups.com
Please just ignore everything I have written on this thread. On my
flight to Munich I had a waaaay better idea for how to approach these
kinds of problems, based on the ability of our language to abstract
over unknown function types. This is going to be cool as hell. I would
love to tell you guys about it, but unfortunately I guess it's
something I need to keep secret for now :-(

Apologies, Bienlein, I would really like to tell you what I'm thinking.

Bienlein

unread,
Oct 22, 2013, 12:41:56 PM10/22/13
to ceylon...@googlegroups.com


Am Dienstag, 22. Oktober 2013 18:01:48 UTC+2 schrieb Gavin King:
Please just ignore everything I have written on this thread. On my
flight to Munich I had a waaaay better idea for how to approach these
kinds of problems, based on the ability of our language to abstract
over unknown function types. This is going to be cool as hell. I would
love to tell you guys about it, but unfortunately I guess it's
something I need to keep secret for now :-(

Apologies, Bienlein, I would really like to tell you what I'm thinking.

Sounds cool. If you drop by Frankfurt on the way back from Munich we can go for a beer or two ;-). Take some time to have a look at Munich when you are there. It's a nice town.

Cheers, Bienlein

Gavin King

unread,
Oct 22, 2013, 1:01:45 PM10/22/13
to ceylon...@googlegroups.com
On Tue, Oct 22, 2013 at 6:41 PM, Bienlein <jet...@web.de> wrote:

> Sounds cool. If you drop by Frankfurt on the way back from Munich we can go
> for a beer or two ;-). Take some time to have a look at Munich when you are
> there. It's a nice town.

Yes, it's my second time here, it's great :-)

We'll have to organize a Ceylon event in Frankfurt. Looks like I have
one in Berlin coming up soon...

David Kerr

unread,
May 3, 2017, 6:19:27 AM5/3/17
to ceylon-users
Has there been any update to delegates in Ceylon since 2013?

Gavin King

unread,
May 3, 2017, 8:16:00 AM5/3/17
to ceylon...@googlegroups.com
Well, I wrote this little library a while back:

https://github.com/gavinking/ceylon.proxy

It's not as cool as what is proposed in that github issue, but it
might be useful.
> --
> 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/39f6c6fc-079b-47c0-93af-b0a46138dbf5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages