September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,4 +1,3 @@
{{omit from|Rust}}
Demonstrate how to dynamically add variables to an object (a class instance) at runtime.
This is useful when the methods/variables of an instance are based on a data file that isn't available until runtime. Hal Fulton gives an example of creating an OO CSV parser at [http://www.devsource.com/article2/0,1759,1928562,00.asp An Exercise in Metaprogramming with Ruby]. This is referred to as "monkeypatching" by Pythonistas and some others.

View file

@ -1,25 +1,25 @@
import extensions.
import extensions;
class Extender :: BaseExtender
class Extender : BaseExtender
{
object prop foo :: theField.
prop object foo;
constructor new : anObject
[
theObject := anObject.
]
constructor(object)
{
theObject := object
}
}
public program
[
var anObject := 234.
public program()
{
var object := 234;
// extending an object with a field
anObject := Extender new(anObject).
object := new Extender(object);
anObject foo := "bar".
object.foo := "bar";
console printLine(anObject,".foo=",anObject foo).
console.printLine(object,".foo=",object.foo);
console readChar
]
console.readChar()
}

View file

@ -0,0 +1,49 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type SomeStruct struct {
runtimeFields map[string]string
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
ss := SomeStruct{make(map[string]string)}
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Create two fields at runtime: ")
for i := 1; i <= 2; i++ {
fmt.Printf(" Field #%d:\n", i)
fmt.Print(" Enter name : ")
scanner.Scan()
name := scanner.Text()
fmt.Print(" Enter value : ")
scanner.Scan()
value := scanner.Text()
check(scanner.Err())
ss.runtimeFields[name] = value
fmt.Println()
}
for {
fmt.Print("Which field do you want to inspect ? ")
scanner.Scan()
name := scanner.Text()
check(scanner.Err())
value, ok := ss.runtimeFields[name]
if !ok {
fmt.Println("There is no field of that name, try again")
} else {
fmt.Printf("Its value is '%s'\n", value)
return
}
}
}