ConcatListOfStrings

One of the slow parts about Copper is string handling within Copper itself. In trying to use Copper for handling strings from files, you may discover there is no object type for handling lists of characters. This is different from strings because lists can grow and strings cannot. Thus, the preferred approach is to create a list containing strings of single characters, and this way the list can grow and shrink and be modified. However, it does bring up the problem: How do we get the list back into being a string? After all, it would be annoying to create loops everywhere (e.g. for printing) just to iterate through all the characters.
I decided to create a simple extension function, not part of the engine but one you will definitely want if you work with strings. It’s called StringListConcat, and in Copper, it is str_list_concat(). The function accepts both strings and lists of strings and combines them into a single string as efficiently as feasible on the C++ side.
Simple example:

a = list("Hello" “World”)
print( str_list_concat(a: “Dog") )

Output: HelloWorldDog

Custom Rouge Themes and How to Fix the Ruby Rouge::Theme uninitialized constant problem

After the most recent changes to the Copper programming language and a switch to a newer computer, I decided to finally rebuild the documentation. I have been using the Slate static site generator creating with Ruby on Rails which uses redcarpet and rouge. Sadly, on a new machine, I had to set up everything again. As you may have discovered, rouge, despite its popularity, has really crappy documentation. Ruby itself is crappy in my opinion and I hate working with it, but there has been some nice stuff created with it. Slate is one example. However, after the headache of trying to dig up the documentation, I may switch to using Copper to generate its own documentation. Not a bad idea, no? Anyways, I decided it would be useful to share my tidbits of knowledge with others who will stumble on this problem while creating custom themes for rouge.

Continue reading “Custom Rouge Themes and How to Fix the Ruby Rouge::Theme uninitialized constant problem”

Class Prototypes

In Javascript, objects and arrays have access via a member to their parent class’s prototype. This allows the user to modify a base property of the class and have it affect all instances. We can do something very similar and very easily in Copper.
Cutting straight to the code, suppose we have an object named Beast, which we copy (via assignment) to make multiple instances:

Beast = [ legs=4 ] { }
rover = Beast
spike = Beast

Both “rover” and “spike” here inherit the member “legs”, which is set to four. Now suppose “rover” loses a leg. We substract one.

–(rover.legs)
# Or rover.legs = 3 #

Then later on “rover” completely heals. How many legs should rover have? We could do…

rover.legs = Beast.legs

But we’re lazy, a real world scenario might be more complicated, and we want to use prototyping. With that in mind, we could set up Beast as follows:

Beast = [
legs = 4,
heals = { super.legs = super.prototype.legs }
] {
this.prototype ~ this,
ret(copy_of(this))
}

This does mean that we need to change our construction of “rover” and “spike” from being direct copies of Beast to being the recipients of function calls to Beast. This pattern may be comfortable to those who prefer constructors, and it allows for initialization parameters.

rover = Beast()
spike = Beast()

Alternatively, we could assign the prototype member of Beast after its construction.

Beast = [
legs = 4
prototype = {}
heals = { super.legs = super.prototype.legs }
]
Beast.prototype ~ Beast

In either case, now we can call the “heals” function on “rover” and let the prototype do the work.

rover.heals()

What about cyclic references?
Copper has no such issue since everything is tied to the global stack. If we were to replace the implementation of Beast with an empty function, all the pointer references would, upon being used, declare the presence of an empty function and terminate. No crash; just a warning message.