Creating a Class>>newX method directly from a pre-existing instance X

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating a Class>>newX method directly from a pre-existing instance X

Erich Groat

Hi all,

I was wondering if there are any built-in facilities for taking a particular instance a class X, call it anX, and creating from it a class method that returns an identical instance of anX for future use.

For example, say I'm futzing around in a workspace with an instance of a class called InteractiveWindowShape, tweaking things so that I have a nice window with all the colors and proportions and buttons I want, perhaps using Morphs to create the thing dynamically (so it's some kind of Morph object). In the course of this I get an instance of anInteractiveWindowShape that I am satisfied with, all the instance variables set just right. Let's call this object | idealShape |. It's just sitting there, rather vulnerably, in a workspace, the result of my using various screen tools over the last half hour. Now, I don't just want to save this object: I want to be able to create an identical one whenever I like. So what I want is a Class method, InteractiveWindowShapes class>>newIdealShape, that creates an instance exactly like idealShape.

Is there a message I can send to this instance idealShape that would return a block of code that would act as a class method, which I could then call newIdealShape, and which would return an identical instance?

I suspect such a general method might not exist, due to the potential hazards surrounding the deep copy problem; I also suspect I may not be imagining the best sort of solution to my problem. Is there a strategy for this?

Thanks all!

Erich

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Creating a Class>>newX method directly from a pre-existing instance X

Chris Cunnington
Well, I don't think you'd programmatically create a new class, because your instance has to have been an instance of a class that already exists. Or else you would not have an instance to begin with. 

Perhaps, you want a way to programatically reproduce an instance you've been working with by hand in a Workspace. You have the instance. It's just right. You want it to be programmatically reproducible at will. I'd use Object>>storeString. 

x := (HelloWorld new) color: 'Black'.

I've created a instance with an instvar. I send it storeString:
x storeString 

and I get: 

'(HelloWorld basicNew instVarAt: 1 put: ''Black''; yourself)'

It's a bit crufty to me. I remove the first and last quote and parentheses. The double single quotes aren't great, so I remove them too. 

HelloWorld basicNew instVarAt: 1 put: 'Black'; yourself

Print it, and get: 

a HelloWorld 

I'm not sure why it comes in some extra syntax. 
That's my best guess at an answer to your question. YMMV. 


Chris 




On 2012-06-09, at 5:04 PM, Erich Groat wrote:


Hi all,

I was wondering if there are any built-in facilities for taking a particular instance a class X, call it anX, and creating from it a class method that returns an identical instance of anX for future use.

For example, say I'm futzing around in a workspace with an instance of a class called InteractiveWindowShape, tweaking things so that I have a nice window with all the colors and proportions and buttons I want, perhaps using Morphs to create the thing dynamically (so it's some kind of Morph object). In the course of this I get an instance of anInteractiveWindowShape that I am satisfied with, all the instance variables set just right. Let's call this object | idealShape |. It's just sitting there, rather vulnerably, in a workspace, the result of my using various screen tools over the last half hour. Now, I don't just want to save this object: I want to be able to create an identical one whenever I like. So what I want is a Class method, InteractiveWindowShapes class>>newIdealShape, that creates an instance exactly like idealShape.

Is there a message I can send to this instance idealShape that would return a block of code that would act as a class method, which I could then call newIdealShape, and which would return an identical instance?

I suspect such a general method might not exist, due to the potential hazards surrounding the deep copy problem; I also suspect I may not be imagining the best sort of solution to my problem. Is there a strategy for this?

Thanks all!

Erich
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Creating a Class>>newX method directly from a pre-existing instance X

Casey Ransberger-2
Maybe worth noting that #storeString might not work so well with a Morph that's visible in the World... IIRC that ends up trying to store the whole World, because Morphs hang onto a reference to their current World.

It will work as long as there aren't cycles and it's not in the current world.

In the menu of a workspace, there's an option to create references from dropped Morphs. I think the way I hustled around the problem was by dropping my handmade Morph into the workspace to get a reference, then detaching the Morph from the world with the halo (the workspace has a reference so the Morph doesn't meet the great garbage collector in the sky.)

#storeString and #storeOn: are fun:)

On Jun 9, 2012, at 2:37 PM, Chris Cunnington <[hidden email]> wrote:

Well, I don't think you'd programmatically create a new class, because your instance has to have been an instance of a class that already exists. Or else you would not have an instance to begin with. 

Perhaps, you want a way to programatically reproduce an instance you've been working with by hand in a Workspace. You have the instance. It's just right. You want it to be programmatically reproducible at will. I'd use Object>>storeString. 

x := (HelloWorld new) color: 'Black'.

I've created a instance with an instvar. I send it storeString:
x storeString 

and I get: 

'(HelloWorld basicNew instVarAt: 1 put: ''Black''; yourself)'

It's a bit crufty to me. I remove the first and last quote and parentheses. The double single quotes aren't great, so I remove them too. 

HelloWorld basicNew instVarAt: 1 put: 'Black'; yourself

Print it, and get: 

a HelloWorld 

I'm not sure why it comes in some extra syntax. 
That's my best guess at an answer to your question. YMMV. 


Chris 




On 2012-06-09, at 5:04 PM, Erich Groat wrote:


Hi all,

I was wondering if there are any built-in facilities for taking a particular instance a class X, call it anX, and creating from it a class method that returns an identical instance of anX for future use.

For example, say I'm futzing around in a workspace with an instance of a class called InteractiveWindowShape, tweaking things so that I have a nice window with all the colors and proportions and buttons I want, perhaps using Morphs to create the thing dynamically (so it's some kind of Morph object). In the course of this I get an instance of anInteractiveWindowShape that I am satisfied with, all the instance variables set just right. Let's call this object | idealShape |. It's just sitting there, rather vulnerably, in a workspace, the result of my using various screen tools over the last half hour. Now, I don't just want to save this object: I want to be able to create an identical one whenever I like. So what I want is a Class method, InteractiveWindowShapes class>>newIdealShape, that creates an instance exactly like idealShape.

Is there a message I can send to this instance idealShape that would return a block of code that would act as a class method, which I could then call newIdealShape, and which would return an identical instance?

I suspect such a general method might not exist, due to the potential hazards surrounding the deep copy problem; I also suspect I may not be imagining the best sort of solution to my problem. Is there a strategy for this?

Thanks all!

Erich
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Creating a Class>>newX method directly from a pre-existing instance X

Ben Coman
Casey Ransberger wrote:
Maybe worth noting that #storeString might not work so well with a Morph that's visible in the World... IIRC that ends up trying to store the whole World, because Morphs hang onto a reference to their current World.

It will work as long as there aren't cycles and it's not in the current world.

In the menu of a workspace, there's an option to create references from dropped Morphs. I think the way I hustled around the problem was by dropping my handmade Morph into the workspace to get a reference, then detaching the Morph from the world with the halo (the workspace has a reference so the Morph doesn't meet the great garbage collector in the sky.)

#storeString and #storeOn: are fun:)

On Jun 9, 2012, at 2:37 PM, Chris Cunnington [hidden email] wrote:

  
Well, I don't think you'd programmatically create a new class, because your instance has to have been an instance of a class that already exists. Or else you would not have an instance to begin with. 

Perhaps, you want a way to programatically reproduce an instance you've been working with by hand in a Workspace. You have the instance. It's just right. You want it to be programmatically reproducible at will. I'd use Object>>storeString. 

x := (HelloWorld new) color: 'Black'.

I've created a instance with an instvar. I send it storeString:
	
x storeString 

and I get: 

'(HelloWorld basicNew instVarAt: 1 put: ''Black''; yourself)'

It's a bit crufty to me. I remove the first and last quote and parentheses. The double single quotes aren't great, so I remove them too. 

HelloWorld basicNew instVarAt: 1 put: 'Black'; yourself

Print it, and get: 

a HelloWorld 

I'm not sure why it comes in some extra syntax. 
That's my best guess at an answer to your question. YMMV. 


Chris 




On 2012-06-09, at 5:04 PM, Erich Groat wrote:

    
Hi all,

I was wondering if there are any built-in facilities for taking a particular instance a class X, call it anX, and creating from it a class method that returns an identical instance of anX for future use.

For example, say I'm futzing around in a workspace with an instance of a class called InteractiveWindowShape, tweaking things so that I have a nice window with all the colors and proportions and buttons I want, perhaps using Morphs to create the thing dynamically (so it's some kind of Morph object). In the course of this I get an instance of anInteractiveWindowShape that I am satisfied with, all the instance variables set just right. Let's call this object | idealShape |. It's just sitting there, rather vulnerably, in a workspace, the result of my using various screen tools over the last half hour. Now, I don't just want to save this object: I want to be able to create an identical one whenever I like. So what I want is a Class method, InteractiveWindowShapes class>>newIdealShape, that creates an instance exactly like idealShape.

Is there a message I can send to this instance idealShape that would return a block of code that would act as a class method, which I could then call newIdealShape, and which would return an identical instance?

I suspect such a general method might not exist, due to the potential hazards surrounding the deep copy problem; I also suspect I may not be imagining the best sort of solution to my problem. Is there a strategy for this?

Thanks all!

Erich
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
      
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
    

  

_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners
You might look at Class Variables.  Use the halos to bring up an Inspector on the top level morph, and within that execute MyClass storeProtoype such that you store the morph you have.  Then later work out how you want to copy generate clones of that object.  For example

MyClass class >> storePrototype: aMorph
    MyClassVariable := aMorph.


MyClass class >> clonePrototype
    ^ MyClassVariable copy.

Also, you might look back at Squeak 3.9 and go...
1. World > Flaps > Supplies
2. The drag your morph into the Supplies flap which should now show a icon looking like you morph.
3. Now drag several of those icons out into the world.

cheer -ben

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners