accepting method without creating variable and code generation
Hi, is it possible to accept a method without creating instance variable? E.g. ~~~~~~~~~~~~~~~~~~~~~~~~~~ Object subclass: #MyObject slots: { } classVariables: { } category: 'Category' ~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValue: aValue container add: aValue ~~~~~~~~~~~~~~~~~~~~~~~~~~ Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called. My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object. Of course I could do ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValueTemplate ^ 'MyObject>>addValue: aValue container add: aValue' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ But then I would lose code highlighting, which is quite error-prone for more complex snippets. If you have a better approach, I am all ears. :) Thanks, Peter
On Tue, Jun 28, 2016 at 5:55 PM, Peter Uhnak <i.uhnak@gmail.com> wrote:
Hi,
is it possible to accept a method without creating instance variable?
E.g.
~~~~~~~~~~~~~~~~~~~~~~~~~~ Object subclass: #MyObject slots: { } classVariables: { } category: 'Category' ~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValue: aValue container add: aValue ~~~~~~~~~~~~~~~~~~~~~~~~~~
Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called.
My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object.
Of course I could do
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValueTemplate ^ 'MyObject>>addValue: aValue container add: aValue' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But then I would lose code highlighting, which is quite error-prone for more complex snippets.
If you have a better approach, I am all ears. :)
Thanks, Peter
If you are only templating the method and not the whole class, why not add it as an instance variable MyObject? Or if MyObject is a real domain object with a few template methods, maybe put the templates on the class side and add a dummy class-instance-variable there. cheers -ben
Yeah I guess that's not such a bad idea, to have a TemplateClass that would contain just the template methods, so I don't need to worry about conflicting instance variables. Peter On Tue, Jun 28, 2016 at 2:08 PM, Ben Coman <btc@openinworld.com> wrote:
On Tue, Jun 28, 2016 at 5:55 PM, Peter Uhnak <i.uhnak@gmail.com> wrote:
Hi,
is it possible to accept a method without creating instance variable?
E.g.
~~~~~~~~~~~~~~~~~~~~~~~~~~ Object subclass: #MyObject slots: { } classVariables: { } category: 'Category' ~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValue: aValue container add: aValue ~~~~~~~~~~~~~~~~~~~~~~~~~~
Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called.
My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object.
Of course I could do
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValueTemplate ^ 'MyObject>>addValue: aValue container add: aValue' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But then I would lose code highlighting, which is quite error-prone for more complex snippets.
If you have a better approach, I am all ears. :)
Thanks, Peter
If you are only templating the method and not the whole class, why not add it as an instance variable MyObject? Or if MyObject is a real domain object with a few template methods, maybe put the templates on the class side and add a dummy class-instance-variable there.
cheers -ben
Still, I think it would be nice to be able to save a method even when it does not compile. On Tue, Jun 28, 2016 at 2:52 PM, Peter Uhnák <i.uhnak@gmail.com> wrote:
Yeah I guess that's not such a bad idea, to have a TemplateClass that would contain just the template methods, so I don't need to worry about conflicting instance variables.
Peter
On Tue, Jun 28, 2016 at 2:08 PM, Ben Coman <btc@openinworld.com> wrote:
On Tue, Jun 28, 2016 at 5:55 PM, Peter Uhnak <i.uhnak@gmail.com> wrote:
Hi,
is it possible to accept a method without creating instance variable?
E.g.
~~~~~~~~~~~~~~~~~~~~~~~~~~ Object subclass: #MyObject slots: { } classVariables: { } category: 'Category' ~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValue: aValue container add: aValue ~~~~~~~~~~~~~~~~~~~~~~~~~~
Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called.
My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object.
Of course I could do
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValueTemplate ^ 'MyObject>>addValue: aValue container add: aValue' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But then I would lose code highlighting, which is quite error-prone for more complex snippets.
If you have a better approach, I am all ears. :)
Thanks, Peter
If you are only templating the method and not the whole class, why not add it as an instance variable MyObject? Or if MyObject is a real domain object with a few template methods, maybe put the templates on the class side and add a dummy class-instance-variable there.
cheers -ben
On 28/06/2016 15:03, Nicolas Passerini wrote:
Still, I think it would be nice to be able to save a method even when it does not compile.
I think Marcus added the possibility to do that in Pharo 6 as a Setting. -- Cyril Ferlicot http://www.synectique.eu 165 Avenue Bretagne Lille 59000 France
Ding dong Marcus! Le 28 juin 2016 15:04, "Nicolas Passerini" <npasserini@gmail.com> a écrit :
Still, I think it would be nice to be able to save a method even when it does not compile.
On Tue, Jun 28, 2016 at 2:52 PM, Peter Uhnák <i.uhnak@gmail.com> wrote:
Yeah I guess that's not such a bad idea, to have a TemplateClass that would contain just the template methods, so I don't need to worry about conflicting instance variables.
Peter
On Tue, Jun 28, 2016 at 2:08 PM, Ben Coman <btc@openinworld.com> wrote:
On Tue, Jun 28, 2016 at 5:55 PM, Peter Uhnak <i.uhnak@gmail.com> wrote:
Hi,
is it possible to accept a method without creating instance variable?
E.g.
~~~~~~~~~~~~~~~~~~~~~~~~~~ Object subclass: #MyObject slots: { } classVariables: { } category: 'Category' ~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValue: aValue container add: aValue ~~~~~~~~~~~~~~~~~~~~~~~~~~
Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called.
My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object.
Of course I could do
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ MyObject>>addValueTemplate ^ 'MyObject>>addValue: aValue container add: aValue' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
But then I would lose code highlighting, which is quite error-prone for more complex snippets.
If you have a better approach, I am all ears. :)
Thanks, Peter
If you are only templating the method and not the whole class, why not add it as an instance variable MyObject? Or if MyObject is a real domain object with a few template methods, maybe put the templates on the class side and add a dummy class-instance-variable there.
cheers -ben
On 28 Jun 2016, at 15:09, Guillermo Polito <guillermopolito@gmail.com> wrote:
Ding dong Marcus!
⦠is on Holidays⦠with the option #optionParseErrors you can compile code with syntax errors: method := Smalltalk compiler class: UndefinedObject; options: #(+ optionParseErrors); compile: 'errorMethod ^1+'. method valueWithReceiver: nil arguments: #() This is really compiled to raise a syntax error at runtime. see OCASTTranslator>>#visitParseErrorNode: anErrorNode methodBuilder pushLiteralVariable: #error -> anErrorNode asSyntaxErrorNotification; send: #signal. with RBParseErrorNode>>#asSyntaxErrorNotification ^SyntaxErrorNotification new setClass: self methodNode methodClass category: nil code: self methodNode source doitFlag: false errorMessage: errorMessage location: self start Marcus
On Tue, Jun 28, 2016 at 9:31 PM, Marcus Denker <marcus.denker@inria.fr> wrote:
On 28 Jun 2016, at 15:09, Guillermo Polito <guillermopolito@gmail.com> wrote:
Ding dong Marcus!
⦠is on Holidays⦠with the option #optionParseErrors you can compile code with syntax errors:
wow! quite an intelligent bot you are there. written in Smalltalk? maybe when Marcus is back you can ask him to share the source ;) cheers -ben
method := Smalltalk compiler class: UndefinedObject; options: #(+ optionParseErrors); compile: 'errorMethod ^1+'. method valueWithReceiver: nil arguments: #() This is really compiled to raise a syntax error at runtime. see
OCASTTranslator>>#visitParseErrorNode: anErrorNode methodBuilder pushLiteralVariable: #error -> anErrorNode asSyntaxErrorNotification; send: #signal.
with
RBParseErrorNode>>#asSyntaxErrorNotification ^SyntaxErrorNotification new setClass: self methodNode methodClass category: nil code: self methodNode source doitFlag: false errorMessage: errorMessage location: self start
Marcus
Le 28/06/2016 15:03, Nicolas Passerini a écrit :
Still, I think it would be nice to be able to save a method even when it does not compile.
I believe it would be better to be able to manipulate, inspect and edit properly virtual methods (MCMethodDefinition, RB-created methods) given that saving a method even if it does not compile is already possible, even if not made available in the GUI. Thierry
On Tue, Jun 28, 2016 at 2:52 PM, Peter Uhnák <i.uhnak@gmail.com <mailto:i.uhnak@gmail.com>> wrote:
Yeah I guess that's not such a bad idea, to have a TemplateClass that would contain just the template methods, so I don't need to worry about conflicting instance variables.
Peter
On Tue, Jun 28, 2016 at 2:08 PM, Ben Coman <btc@openinworld.com <mailto:btc@openinworld.com>> wrote:
On Tue, Jun 28, 2016 at 5:55 PM, Peter Uhnak <i.uhnak@gmail.com <mailto:i.uhnak@gmail.com>> wrote: > Hi, > > is it possible to accept a method without creating instance variable? > > E.g. > > ~~~~~~~~~~~~~~~~~~~~~~~~~~ > Object subclass: #MyObject > slots: { } > classVariables: { } > category: 'Category' > ~~~~~~~~~~~~~~~~~~~~~~~~~~ > > ~~~~~~~~~~~~~~~~~~~~~~~~~~ > MyObject>>addValue: aValue > container add: aValue > ~~~~~~~~~~~~~~~~~~~~~~~~~~ > > Now normally when I would try to compile the method I would get the "Unknown variable 'container'" warning that will force me to either create temporary or instance variable; I would like to somehow ignore that, because the method will actually never get called. > > My objective is use this method as a template for code generation, so I would then take this method, apply some code transformation and compile it into different object. > > Of course I could do > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > MyObject>>addValueTemplate > ^ 'MyObject>>addValue: aValue > container add: aValue' > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > But then I would lose code highlighting, which is quite error-prone for more complex snippets. > > If you have a better approach, I am all ears. :) > > Thanks, > Peter
If you are only templating the method and not the whole class, why not add it as an instance variable MyObject? Or if MyObject is a real domain object with a few template methods, maybe put the templates on the class side and add a dummy class-instance-variable there.
cheers -ben
participants (8)
-
Ben Coman -
Cyril Ferlicot Delbecque -
Guillermo Polito -
Marcus Denker -
Nicolas Passerini -
Peter Uhnak -
Peter Uhnák -
Thierry Goubier