Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Created March 13, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyvanakbary/4e3ccda72c169dcaf1b3 to your computer and use it in GitHub Desktop.
Save keyvanakbary/4e3ccda72c169dcaf1b3 to your computer and use it in GitHub Desktop.
Modelling objects
(define (create-user name)
(define say-hello
(print "Hello " name))
(define (change-name new-name)
(set! name new-name)
name)
(define (dispatch method)
(cond
((eq? method 'say-hello) say-hello)
((eq? method 'change-name) change-name)
(else (raise "Invalid method"))))
dispatch)
(define user (create-user "Keyvan"))
(user 'say-hello)
; Hello Keyvan
((user 'change-name) "John")
(user 'say-hello)
; Hello John
@sergigp
Copy link

sergigp commented Mar 13, 2016

solo por ver como quedaba te lo he picado en scala xD

case class InmutableUser(name: String) ={
def sayHello:Unit = println s"hello $name"
def changeName(name: String): User = copy(name = name)
}

case class MutableUser(var name: String) ={
def sayHello:Unit = println s"hello $name"
def changeName(newName: String): Unit = name = newName
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment