dylan


standard (idiomatic) way of convert an object to string


I tried to print an object of this class:
define class <bill> (<object>)
slot num :: <string>;
slot ref :: <string>;
end class <bill>;
I can make a method like say
define method say (bill : <bill>) => ();
format-out("num:%s\n", bill.num);
format-out("ref:%s\n", bill.ref);
end method say;
but I want a method to convert the object to <string> that can be used like
format-out("%s\n", bill);
or
format-out("%s\n", as(<string>, bill));
or
format-out("%=\n", bill);
I tried
define method as(type == <string>, bill :: <bill>)
=> (s :: <string>);
let result :: <stretchy-vector> = make(<stretchy-vector>);
add!(result, bill.num);
add!(result, bill.ref);
join(result, " ");
end method as;
but with:
format-out("\%s\n", as(<string>,bill));
I had this result
Object {<bill>} cannot be converted to class {<class>: <string>}
Object {<bill> 0x1ff8180} cannot be converted to class {class <string> 0x7f5d5c645c00}
Breaking into debugger.
Trace/breakpoint trap
An with this:
format-out("%s\n", bill);
that result:
{<bill> #x00000000025EF1B0}
What I'm doing wrong?
What is the standard (idiomatic) way of printing an object?
The idiomatic way to adding support for printing an object is by specializing print-object. But if you want to be able to convert object representation to string, specializing as generic is the way to go, as you did. It works here, so maybe there's a bug somewhere:
module: bill
define class <bill> (<object>)
constant slot num :: <string>, init-keyword: num:;
constant slot ref :: <string>, init-keyword: ref:
end class <bill>;
define method as(type == <string>, bill :: <bill>) => (s :: <string>);
join(vector(bill.num, bill.ref), " ");
end method as;
format-out("%=\n", as(<string>, make(<bill>, num: "1", ref: "a")));
Result:
$ bill
"1 a"

Related Links

Using Dylan to Write a Parser
standard (idiomatic) way of convert an object to string
I can't run any project in openDylan

Categories

HOME
asterisk
qlikview
marie
pelican
grpc
android-ndk
glsl
readdir
spotfire
bitbucket
datetimepicker
ms-application-insights
localhost
sugarcrm
zabbix
samsung-gear-s2
aws-sdk-cpp
dynamics-crm-2011
camunda
google-closure-compiler
k-means
pylint
legend-properties
sonarlint
turn.js
angular2-pipe
inline-assembly
concatenation
scalatest
traveling-salesman
large-files
meteor-accounts
recreate
login-required
visual-studio-debugging
loopback
log4net-configuration
matlab-compiler
android-geofence
dom4j
rythm
realloc
origami
jquery-inputmask
redbean
aura-framework
jquery-ui-slider
definition
f#-fake
grails-2.0
npoco
serverless-architecture
stream-socket-client
confluent
emacs24
webmatrix
google-scholar
huawei
breadcrumbs
firebase-job-dispatcher
git-push
nsoperationqueue
angular-i18n
oryx
updating
jain-sip
jsoncpp
p4python
euro
log-analysis
grails-3.0.9
data-representation
tinymce-plugins
system.diagnostics
httpmodule
nfa
jad
quickfixn
fscanf
switchpreference
atg-dynamo
opencobol
rssi
iwork
data-layers
autofilter
facebook-likebox
maven-javadoc-plugin
cocotron
junction-table
nsautoreleasepool
pageload
database-restore
ncml
esi
shapado
android-memory
idtabs
cgaffinetransform
discussion-board
pkcs#7
software-update
j++

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App