Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create selector dynamically from string

I've made a program that uses reflection to add Traits dynamically, and solves conflicts automatically in one predeterminated way.

It uses aliases. It's working (I think), but I have only a problem when finally adding the trait.

My program generates all the aliases for each conflicting method, and add them with the trait to the class. The problem is that I'm not able to generate the selector correctly, its generating a string instead.

For example:

I need this

TCircle @ {#circleHash -> #hash}

but I'm generating this

TCircle @ {'#circleHash' -> #hash}

you can see the quotes in #circleHash.

Because is a meta-program, it generates also dynamically the selector. How I can get it without the quotes and with the #?

I need to able to do something like this

"have the selector name in string"
obj := 'SelectorDinamicallyGenerated'.
^(#obj)

and get #SelectorDinamicallyGenerated, and not '#SelectorDinamicallyGenerated'.

How can I do this?

I've tried doing like that (#obj) but it is not working (getting #obj)

like image 348
Gonzalo.- Avatar asked Jan 14 '23 12:01

Gonzalo.-


2 Answers

I've found it.

It's

obj asSymbol
like image 78
Gonzalo.- Avatar answered Jan 16 '23 02:01

Gonzalo.-


Good you found it yourself. Maybe it is just irritating that in smalltalk a symbol is a selector. It is just not the case that there is a selector class and you could do "aString asSelector". So

'foo' asSymbol => #foo

will do. If you need to generate a setter you can do

'foo' asSymbol asMutator => #foo: 
like image 27
Norbert Hartl Avatar answered Jan 16 '23 02:01

Norbert Hartl