* Working with string keys in a java.util.Map
@ 2020-07-03 21:46 Duncan Mak
2020-07-03 21:52 ` Sudarshan S Chawathe
0 siblings, 1 reply; 7+ messages in thread
From: Duncan Mak @ 2020-07-03 21:46 UTC (permalink / raw)
To: kawa mailing list
Hello all,
I've been playing around with the java.nio APIs.
Using Files:readAttributes, you can get a Map<String, Object> of attributes.
#|kawa:80|# attrs
{owner=duncan, lastAccessTime=2020-07-03T00:12:53.682826Z,
lastModifiedTime=2019-04-22T18:53:02.529968Z, gid=20,
creationTime=2019-04-22T18:53:02Z, isRegularFile=true, nlink=1,
fileKey=(dev=1000007,ino=12896309012), ino=12896309012, mode=33261,
uid=501, size=20799, dev=16777223, isSymbolicLink=false,
permissions=[GROUP_READ, OWNER_READ, OTHERS_READ, OTHERS_EXECUTE,
OWNER_WRITE, GROUP_EXECUTE, OWNER_EXECUTE], rdev=0,
ctime=2019-04-22T18:53:02.533688Z, isOther=false, isDirectory=false,
group=staff}
#|kawa:81|# (attrs:containsKey "owner")
#f
#|kawa:82|# (attrs:get "owner")
#!null
#|kawa:83|# (attrs:get "gid")
#!null
I think this is failing because the literal string that I typed in is a
gnu.lists.IString (https://www.gnu.org/software/kawa/Strings.html) and I
don't see a function to force any one of Kawa's many string representations
into a java.lang.String.
--
Duncan.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-03 21:46 Working with string keys in a java.util.Map Duncan Mak
@ 2020-07-03 21:52 ` Sudarshan S Chawathe
2020-07-03 22:33 ` Per Bothner
0 siblings, 1 reply; 7+ messages in thread
From: Sudarshan S Chawathe @ 2020-07-03 21:52 UTC (permalink / raw)
To: Duncan Mak; +Cc: kawa mailing list
I've had to get Java "String"s from Kawa strings often for similar
reasons when using other JVM libraries, etc., and I have just used
'(String "kawa string)'; it seems to work well, although it does
introduce clutter.
Regards,
-chaw
> Date: Fri, 3 Jul 2020 17:46:19 -0400
> Subject: Working with string keys in a java.util.Map
> To: kawa mailing list <kawa@sourceware.org>
> From: Duncan Mak via Kawa <kawa@sourceware.org>
>
> Hello all,
>
> I've been playing around with the java.nio APIs.
>
> Using Files:readAttributes, you can get a Map<String, Object> of attributes.
>
> #|kawa:80|# attrs
> {owner=duncan, lastAccessTime=2020-07-03T00:12:53.682826Z,
> lastModifiedTime=2019-04-22T18:53:02.529968Z, gid=20,
> creationTime=2019-04-22T18:53:02Z, isRegularFile=true, nlink=1,
> fileKey=(dev=1000007,ino=12896309012), ino=12896309012, mode=33261,
> uid=501, size=20799, dev=16777223, isSymbolicLink=false,
> permissions=[GROUP_READ, OWNER_READ, OTHERS_READ, OTHERS_EXECUTE,
> OWNER_WRITE, GROUP_EXECUTE, OWNER_EXECUTE], rdev=0,
> ctime=2019-04-22T18:53:02.533688Z, isOther=false, isDirectory=false,
> group=staff}
>
> #|kawa:81|# (attrs:containsKey "owner")
> #f
> #|kawa:82|# (attrs:get "owner")
> #!null
> #|kawa:83|# (attrs:get "gid")
> #!null
>
> I think this is failing because the literal string that I typed in is a
> gnu.lists.IString (https://www.gnu.org/software/kawa/Strings.html) and I
> don't see a function to force any one of Kawa's many string representations
> into a java.lang.String.
>
> --
> Duncan.
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-03 21:52 ` Sudarshan S Chawathe
@ 2020-07-03 22:33 ` Per Bothner
2020-07-04 1:30 ` Jamison Hope
0 siblings, 1 reply; 7+ messages in thread
From: Per Bothner @ 2020-07-03 22:33 UTC (permalink / raw)
To: kawa
On 7/3/20 2:52 PM, Sudarshan S Chawathe wrote:
> I've had to get Java "String"s from Kawa strings often for similar
> reasons when using other JVM libraries, etc., and I have just used
> '(String "kawa string)'; it seems to work well, although it does
> introduce clutter.
I don't see how to avoid that in general. One could write a wrapper Map
that uses a coercion function to convert the key to required type,
similar to the coercion function in the make-parameter function.
Another way is to call the toString method, since Kawa strings
implement java.lang.CharSequence:
("kstring":toString)
This is cheap if the Kawa string is an IString, since uses a java.lang.String.
Alternative syntax:
(->java.lang.String "kstring")
or shorter:
(->String "kstring")
--
--Per Bothner
per@bothner.com http://per.bothner.com/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-03 22:33 ` Per Bothner
@ 2020-07-04 1:30 ` Jamison Hope
2020-07-04 4:04 ` Duncan Mak
0 siblings, 1 reply; 7+ messages in thread
From: Jamison Hope @ 2020-07-04 1:30 UTC (permalink / raw)
To: kawa
In this case, I think another option is to define a wrapper function that
specifies the argument type as String, like:
(define (get attrs key::String) (attrs:get key))
Then (get attrs “owner”) ought to work with the literal converted to
java.lang.String.
-J
On Fri, Jul 3, 2020 at 6:33 PM Per Bothner <per@bothner.com> wrote:
> On 7/3/20 2:52 PM, Sudarshan S Chawathe wrote:
> > I've had to get Java "String"s from Kawa strings often for similar
> > reasons when using other JVM libraries, etc., and I have just used
> > '(String "kawa string)'; it seems to work well, although it does
> > introduce clutter.
>
> I don't see how to avoid that in general. One could write a wrapper Map
> that uses a coercion function to convert the key to required type,
> similar to the coercion function in the make-parameter function.
>
> Another way is to call the toString method, since Kawa strings
> implement java.lang.CharSequence:
> ("kstring":toString)
>
> This is cheap if the Kawa string is an IString, since uses a
> java.lang.String.
>
> Alternative syntax:
>
> (->java.lang.String "kstring")
> or shorter:
> (->String "kstring")
> --
> --Per Bothner
> per@bothner.com http://per.bothner.com/
>
--
Jamison Hope
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-04 1:30 ` Jamison Hope
@ 2020-07-04 4:04 ` Duncan Mak
2020-07-04 4:29 ` Sudarshan S Chawathe
2020-07-04 5:24 ` Per Bothner
0 siblings, 2 replies; 7+ messages in thread
From: Duncan Mak @ 2020-07-04 4:04 UTC (permalink / raw)
To: Jamison Hope; +Cc: kawa
What do you think of having literal syntax for this -- what about $"this is
a java string"?
This is kinda inspired by what Scala did a little while again:
https://docs.scala-lang.org/overviews/core/string-interpolation.html
Looks like Gauche decided to do something similar:
https://practical-scheme.net/gauche/man/gauche-refe/Strings.html#String-interpolation
Gauche's design look quite nice:
#"This is Gauche, version ~(gauche-version)."
⇒ "This is Gauche, version 0.9.9."
On Fri, Jul 3, 2020 at 9:31 PM Jamison Hope <jhope@alum.mit.edu> wrote:
> In this case, I think another option is to define a wrapper function that
> specifies the argument type as String, like:
>
> (define (get attrs key::String) (attrs:get key))
>
> Then (get attrs “owner”) ought to work with the literal converted to
> java.lang.String.
>
> -J
>
> On Fri, Jul 3, 2020 at 6:33 PM Per Bothner <per@bothner.com> wrote:
>
> > On 7/3/20 2:52 PM, Sudarshan S Chawathe wrote:
> > > I've had to get Java "String"s from Kawa strings often for similar
> > > reasons when using other JVM libraries, etc., and I have just used
> > > '(String "kawa string)'; it seems to work well, although it does
> > > introduce clutter.
> >
> > I don't see how to avoid that in general. One could write a wrapper Map
> > that uses a coercion function to convert the key to required type,
> > similar to the coercion function in the make-parameter function.
> >
> > Another way is to call the toString method, since Kawa strings
> > implement java.lang.CharSequence:
> > ("kstring":toString)
> >
> > This is cheap if the Kawa string is an IString, since uses a
> > java.lang.String.
> >
> > Alternative syntax:
> >
> > (->java.lang.String "kstring")
> > or shorter:
> > (->String "kstring")
> > --
> > --Per Bothner
> > per@bothner.com http://per.bothner.com/
> >
> --
> Jamison Hope
>
--
Duncan.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-04 4:04 ` Duncan Mak
@ 2020-07-04 4:29 ` Sudarshan S Chawathe
2020-07-04 5:24 ` Per Bothner
1 sibling, 0 replies; 7+ messages in thread
From: Sudarshan S Chawathe @ 2020-07-04 4:29 UTC (permalink / raw)
To: Duncan Mak; +Cc: Jamison Hope, kawa
This is only my personal preference but, for what it's worth, I prefer
not having special syntax for such things. I think my earlier message
may have inadvertently suggested that I thought having to write (String
"foo") to get a Java String is a burden; in reality I think it is quite
reasonable.
What I often do is define interface procedures between my code and
whatever Java library I am using and declare the arguments of those
procedures with type annotations (String, in this case) and then Kawa
takes care of the conversion from Kawa string to Java String when I
invoke the procedure. And Kawa's type checking is handy in catching my
errors too, as an added benefit.
Regards,
-chaw
> Date: Sat, 4 Jul 2020 00:04:39 -0400
> Subject: Re: Working with string keys in a java.util.Map
> To: Jamison Hope <jhope@alum.mit.edu>
> From: Duncan Mak via Kawa <kawa@sourceware.org>
> Reply-To: Duncan Mak <duncanmak@gmail.com>
> Cc: "kawa@sourceware.org" <kawa@sourceware.org>
>
> What do you think of having literal syntax for this -- what about $"this is
> a java string"?
>
> This is kinda inspired by what Scala did a little while again:
> https://docs.scala-lang.org/overviews/core/string-interpolation.html
>
> Looks like Gauche decided to do something similar:
> https://practical-scheme.net/gauche/man/gauche-refe/Strings.html#String-int=
> erpolation
>
> Gauche's design look quite nice:
>
> #"This is Gauche, version ~(gauche-version)."
> =E2=87=92 "This is Gauche, version 0.9.9."
>
>
> On Fri, Jul 3, 2020 at 9:31 PM Jamison Hope <jhope@alum.mit.edu> wrote:
>
> > In this case, I think another option is to define a wrapper function that
> > specifies the argument type as String, like:
> >
> > (define (get attrs key::String) (attrs:get key))
> >
> > Then (get attrs =E2=80=9Cowner=E2=80=9D) ought to work with the literal c=
> onverted to
> > java.lang.String.
> >
> > -J
> >
> > On Fri, Jul 3, 2020 at 6:33 PM Per Bothner <per@bothner.com> wrote:
> >
> > > On 7/3/20 2:52 PM, Sudarshan S Chawathe wrote:
> > > > I've had to get Java "String"s from Kawa strings often for similar
> > > > reasons when using other JVM libraries, etc., and I have just used
> > > > '(String "kawa string)'; it seems to work well, although it does
> > > > introduce clutter.
> > >
> > > I don't see how to avoid that in general. One could write a wrapper Ma=
> p
> > > that uses a coercion function to convert the key to required type,
> > > similar to the coercion function in the make-parameter function.
> > >
> > > Another way is to call the toString method, since Kawa strings
> > > implement java.lang.CharSequence:
> > > ("kstring":toString)
> > >
> > > This is cheap if the Kawa string is an IString, since uses a
> > > java.lang.String.
> > >
> > > Alternative syntax:
> > >
> > > (->java.lang.String "kstring")
> > > or shorter:
> > > (->String "kstring")
> > > --
> > > --Per Bothner
> > > per@bothner.com http://per.bothner.com/
> > >
> > --
> > Jamison Hope
> >
>
>
> --=20
> Duncan.
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Working with string keys in a java.util.Map
2020-07-04 4:04 ` Duncan Mak
2020-07-04 4:29 ` Sudarshan S Chawathe
@ 2020-07-04 5:24 ` Per Bothner
1 sibling, 0 replies; 7+ messages in thread
From: Per Bothner @ 2020-07-04 5:24 UTC (permalink / raw)
To: Duncan Mak; +Cc: kawa
On 7/3/20 9:04 PM, Duncan Mak via Kawa wrote:
> What do you think of having literal syntax for this -- what about $"this is
> a java string"?
No, I don't think we need or want a special literal syntax for Java strings.
> This is kinda inspired by what Scala did a little while again:
> https://docs.scala-lang.org/overviews/core/string-interpolation.html
You might want to check out:
https://www.gnu.org/software/kawa/String-literals.html#String-templates
--
--Per Bothner
per@bothner.com http://per.bothner.com/
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2020-07-04 5:24 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-03 21:46 Working with string keys in a java.util.Map Duncan Mak
2020-07-03 21:52 ` Sudarshan S Chawathe
2020-07-03 22:33 ` Per Bothner
2020-07-04 1:30 ` Jamison Hope
2020-07-04 4:04 ` Duncan Mak
2020-07-04 4:29 ` Sudarshan S Chawathe
2020-07-04 5:24 ` Per Bothner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).