Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Getting inherited Superclass methods to return an instance of the Subclass?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Haoyi Li  
View profile  
 More options Mar 4, 12:13 pm
From: Haoyi Li <haoyi...@gmail.com>
Date: Sun, 4 Mar 2012 12:13:15 -0500
Local: Sun, Mar 4 2012 12:13 pm
Subject: Getting inherited Superclass methods to return an instance of the Subclass?

Hey All,

I'm trying to write a simple HTML-generating internal DSL, that uses method
chaining to augment the nodes as I generate them. For example:

nodes =
   div.cls("bordered").background_color("yellow")(
      p.line_height(13).color("blue")(
         hello world
      )
   )

would generate

<div class="bordered" style="background-color: yellow">
    <p style="line-height: 13px; color: blue">
         hello world
    </p>
</div>

The idea is that each method call will augment the item it was called on.
Internally, this is pretty straightforward:

class AnyNode(...){
   def cls(v: String) = this.copy(classes = v :: this.classes)

}

Where each call does a copy-and-update, returning the newly created object.

My problem is that some methods only exist on sub-classes. For example, I
want to be able to do

table.head(...).body(...)

while disallowing

div.head(...).body(...)

Hence "head()" is defined as a method of the Table class, and not of the
AnyNode class. However, all the generic operations are defined on the
AnyNode class, and thus return objects of type AnyNode. As a result, I can
do this:

table.head(...).cls("bordered")

but I cannot do this

table.cls("bordered").head(...)

because cls returns an AnyNode. Is there any way to get cls (which Table
inherited from AnyNode), when called on a instance of Table, to return
another instance of Table? Or is it impossible, and should I be trying a
different approach to this?

Thanks!
-Haoyi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jan Vanek  
View profile  
 More options Mar 4, 1:10 pm
From: Jan Vanek <j3va...@googlemail.com>
Date: Sun, 04 Mar 2012 19:10:24 +0100
Local: Sun, Mar 4 2012 1:10 pm
Subject: Re: [scala-user] Getting inherited Superclass methods to return an instance of the Subclass?
Use this.type.

Example:

class Node {
     def cls(c: String): this. type = { whatever; this }

}

Regards,
Jan

On 04.03.2012 18:13, Haoyi Li wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roland Kuhn  
View profile  
 More options Mar 4, 1:21 pm
From: Roland Kuhn <goo...@rkuhn.info>
Date: Sun, 4 Mar 2012 19:21:58 +0100
Local: Sun, Mar 4 2012 1:21 pm
Subject: Re: [scala-user] Getting inherited Superclass methods to return an instance of the Subclass?

On Mar 4, 2012, at 19:10 , Jan Vanek wrote:

> Use this.type.

> Example:

> class Node {
>    def cls(c: String): this. type = { whatever; this }
> }

this.type means “exactly this object”, which does not fit the OP’s requirements (return updated copy). Unfortunately, the issue is not a simple one, search this group for MyType. In essence you would need to go the same route as the collections library (using the CanBuildFrom pattern).

Regards,

Roland

Roland Kuhn
Typesafe – The software stack for applications that scale.
twitter: @rolandkuhn

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomas Mikula  
View profile  
 More options Mar 5, 1:57 pm
From: Tomas Mikula <tomas.mik...@gmail.com>
Date: Mon, 5 Mar 2012 10:57:59 -0800 (PST)
Local: Mon, Mar 5 2012 1:57 pm
Subject: Re: Getting inherited Superclass methods to return an instance of the Subclass?
You can add a type parameter to AnyNode that represents the actual
subclass:

abstract class AnyNode[Self](...){
    def copy(...): Self
    def cls(v: String): Self = ...

}

class Table extends AnyNode[Table](...) {
    ...

}

Regards,
Tomas

On Mar 4, 6:13 pm, Haoyi Li <haoyi...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »