Rename or alias method?

Is there a simple way to rename or make an alias of a method? For instance, if I wanted to rename the .play method everywhere to .foobar?

+Object {
   foobar {
      |...args|
      ^this.play(*args)
   }
}

This doesnā€™t remove existing play methods however.

1 Like

Great, thanks! Actually, thatā€™s fine, I wouldnā€™t want to remove the existing method.

What does the * signify in this setting?

It allows every element in array to be unpacked & passed successively as comma separated values.

One way is:

this.preProcessor_
{
	|code| if(code.contains(".alias")){code=code.replace(".alias",".play")}
} 

;{	Saw.ar	}.alias
1 Like

foobar will keep args in an array. The * passes them as single arguments to play rather than passing an array of arguments.

1 Like

Ok, I should let on what Iā€™m up to! For a particular project, I wanted to see if I could livecode in a sort of reverse ā€˜Gringlishā€™ ā€“ using the Greek alphabet to code in instead of Latin.

This didnā€™t parse:

   Ļ€Ī»Ī±Ļ… {
      |...args|
      ^this.Ļ€Ī»Ī±Ļ…(*args)
   }
}

But this does :slight_smile:

this.preProcessor_
{
	|code| if(code.contains(".Ļ€Ī»Ī±Ļ…")){code=code.replace(".Ļ€Ī»Ī±Ļ…",".play")}
}
)

().Ļ€Ī»Ī±Ļ…

Iā€™ve also been able to use EventShortcuts from the miSCellaneous library to be able, for instance, to write \amp in an event as Ī±ā€¦

2 Likes

Thatā€™s sickā€¦ check this out:

A live coding language written in one-line:

this.preProcessor=PreProcessor.new.startDelimiter_("*:").endDelimiter_(":*");x=(lang:\xoxo,languages:(xoxo:{|c,e|var v=c.split($:),o=Pbind(\amp,Pseq(v[1].asList.collect{|q|(q.asString=="x").binaryValue},inf),\instrument,v[0].asSymbol,\dur,1/4);e.put(\xo,o)}))

It requires PreProcessor, this is for you.

Iā€™m not currently aware of any embedded live-coding languages using only the core library.

Iā€™ve also never ventured far into these waters, and so I decided to attempt it using this as an example.

This wasā€¦ difficult, even though it looks simple.

It appears critical for this.preProcessor to return itā€™s argument value.

s.boot
;
this.preProcessor_{|code| 
	
	if( code includesAny:
		[
			$Ī» , $Ī± , $Ļ… , $Ļ€
		],
		{[
			".Ļ€Ī»Ī±Ļ…" , ".play",
			".Ļ…Ī±Ī»Ļ€" , ".next"
		]
		.pairsDo
		{
			|key val| code=replace(code,key,val)
		}
	})
	
	;code.inform 
	// ;code /*critical*/
}
;
{Saw.ar}.Ļ€Ī»Ī±Ļ…
;
{Saw.ar}.play

I was groping in the dark until I discovered itā€™s ending resolution by perfect chanceā€¦

Not exactly ā€“ if you return the input argument value, then the interpreter will try to run the original string, which will choke on nonstandard syntax.

The preprocessor should return a string containing the final code to execute. (Admittedly this is not stated directly in the documentation ā€“ Iā€™m surprised to see that actually.)

In the posted example, the value of the code variable is being modified. So returning code from the function is returning the new string ā€“ and not the argument value.

Just wish to be precise, to avoid confusion.

hjh

Iā€™m glad to see that this odd project of mine has interest from people much better at coding than I am! Iā€™ll work through the suggestions here when I get time and see how I get on.

I canā€™t quite make head or tail of that quark, but Rainerā€™s core library solution is working for me so far, thanks!

Two things I donā€™t understand. Why do we need the if? Secondly what is the meaning of the $ in $Ī»?

the $ is how you indicate a character. ā€œrā€ is a String of length 1. its first element is Char($r)

a = "r";
a.class // String
a // r
a[0] // r
a[0].class // Char

the if clause is optional - if the string you hope to replace is present it wastes a few cycles, if not it saves a few

@semiquaver answered perfectlyā€¦The if conditional is included as an optional efficiency statement, and while it may be slightly verbose for brief demonstrations, it could also allow one to sensibly introduce such an alias set into a more expanded workflow.

This has the potential to yield excellent resultsā€¦ especially in the case of using a character set that SC would not understand in any other context.

The characters used here (with the if, mandatory) would make a very efficient set of custom commands, in startup.scd.

Itā€™s very difficult to decipherā€¦ Iā€™ve come to understand that it mostly extends the core .preProcessor functionality, by allowing one to define a hierarchy of languages embedded within languages, as well as the ability to call specific translations, without necessarily (I believe) affecting the client/interpreter .preProcessor directly.

Itā€™s good to know every optionā€¦ but unfortunately that quark is rather poorly documented, and far beyond your current use case (it seems) at any rate.