[Pharo-users] Metaprogramming facilities

Peter Uhnák i.uhnak at gmail.com
Tue May 3 15:48:43 EDT 2016


1) write them by hand

annoying typing required

2) use Class refactorings (right click on class -> refactoring -> class
refactoring -> generate accessors; or ctrl+h+a)

this will ask you to choose for all attributes… good initially, but really
stupid if you are providing e.g. just getters (because then you would need
to cancel genreation for all the attributes)

3) use attribute refactorings (right click on class -> refactoring -> inst
var refactoring -> accessors -> select attribute)

good for individual attributes

4) write the code compilation (ideally as part of some tool)

MyClass compile: 'attr ^ attr' classified: 'accessing'.
MyClass compile: 'attr: anObject attr := anObject' classified: 'accessing'.

Much more typing, but good if you have it as part of some tool (so you can
quickly script a gui that would generate it just by clicking on a checkbox
or something)

5) use refactoring (this is what the System Browser uses internally)

(RBCreateAccessorsForVariableRefactoring
variable: #attr class: MyClass classVariable: false) execute

6) use TDD (test driven development)

the Debugger will offer you to create the method and you can modify it as
you work through (I don't know if TDD is already part of the broadcasted
MOOC courses)


Typing by hand is annoying, but it's literally two words for getter and
four for setter.
This mail is probably more words than all the accessors I've created this
year… so typing it by hand is no big deal.
More importantly the autogenerated code is a bit stupid

TBPost >> title: anObject
    title := anObject

this is stupid, so even for generated one I select the argument, press
ctrl+t (or right click + suggestions) and select 'rename' ... and rename it
into something meaningful like

TBPost >> title: aString
    title := aString


But otherwise I regularly use all 1, 2, 4, 5, 6…

I never use 3) it's too annoying and distracting because I have to read the
labels… I can type in the getter/setter without breaking focus on what I am
doing… but it just gave me an idea... :)

Peter

On Tue, May 3, 2016 at 9:05 PM, stepharo <stepharo at free.fr> wrote:

> Code not tested but ....
>
>
> #('title' 'text')
>
>     do: [:each |
>
>            | body |
>
>             body := String new streamContents:
>
>                     [ :s |
>
>                     s nextPutAll: each ; cr; tab;
>
>                         nextPutAll:  '^ ';
>
>                         nextPutAll: each ; cr].
>
>             TBPost compile: body classified: 'accessing'
>
> ]
>
> Le 2/5/16 à 17:35, Victor RENE a écrit :
>
> In the TinyBlog mini-project of the MOOC, we start by creating the model.
> However, since I basically finished the exercises, I wanted to go one step
> further and
> ask you guys, what is the correct way _not_ to write all gettters /
> setters for the fields.
>
>
> TBPost >> title
>     ^ title
> TBPost >> title: anObject
>     title := anObject
> TBPost >> text
>     ^ text
> TBPost >> text: anObject
>     text := anObject
> etc...
>
>
> Do you do it with macros / templates / something else ?
> I am having a bit of fun (although I would love that they unlock weeks 2
> to 7 for me). Thanks in advance.
>
> Victor RENE
> Software engineer,
> Game designer, Writer
>
> Blog: <http://victor-rene.com>http://victor-rene.com
> Projects: <http://logiqub.com>http://logiqub.com
>
> tel: +33 6 26 83 61 76
> email: victor-rene at outlook.com
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20160503/005beea3/attachment.html>


More information about the Pharo-users mailing list