stevied | got another question. let's say I got a method and I want it to be able to accept `5` as an argument or `5.0` as an argument. If I set the parameter to an `Int` it chokes when passed a decimal. When I set the paramter to `Rat` it chokes on an integer value. So how do I best accomplish this? Do I need a `multi` and convert the argument as needed or is there some type that can accept either something th | 00:26 | |
got another question. let's say I got a method and I want it to be able to accept `5` as an argument or `5.0` as an argument. If I set the parameter to an `Int` it chokes when passed a decimal value. When I set the paramter to `Rat` it chokes on an integer value. So how do I best accomplish this? Do I need a `multi` and convert the argument as needed or is there some type that can accept either someth | 00:27 | ||
Nemokosch | depends if you want to convert to a certain type or not | 00:32 | |
stevied | i ultimately want it to be converted to a `Rat` | 00:33 | |
Nemokosch | If you want Rat either way, you could just use Rat() $parameter | ||
the parens allow for conversions | |||
I think this could be further specialized | 00:35 | ||
stevied | oh, wow, never knew that either. | ||
Nemokosch | like what function to use for conversion and stuff | ||
but I rarely wanted anything more complex than this | |||
stevied | yup, that worked. thanks! | ||
sabas3dgh | Hello | 07:08 | |
I need to write a rest wbservice in raku. Is it possible? Please explain! | 07:09 | ||
08:38
Kaiepi left
09:03
Kaiepi joined
|
|||
Nemokosch | Check out Cro | 09:04 | |
10:06
frost left
10:49
lizmat joined
|
|||
stevied | I'm getting an error with this code: paste.debian.net/1243011/ | 10:55 | |
`Illegally post-declared type: Kg used at line 23` | |||
lizmat | stevied: you're using Kg *before* it is being defined | 11:05 | |
you will need to stub the class | |||
e.g. by adding "class Kg { ... }" before class Unit is being defined | |||
after that, you also need to change $to.get_abbr to $to!get_abbr | 11:06 | ||
although I thing you can just replace that by $!abbr :-) | 11:07 | ||
stevied | i tried that, then I get a different error: "No such method 'get_abbr' for tpe 'Kg' | ||
i tried that, then I get a different error: "No such method 'get_abbr' for type 'Kg' | |||
ah, oops | |||
thanks! | |||
lizmat | *think | 11:08 | |
stevied | if raku does a calculation and comes up with an answer of 0.000005, how can I tell it do display that in scientific noatation with "X" number of significant digits? | 12:38 | |
do I need a module to help me do that? | |||
ok sprintf can handle this with "e" format spcificer | 12:49 | ||
lizmat | yup, and there's .fmt (which is essentially sprintf under the hood) | 12:55 | |
m: say 42.fmt("%e") | 12:56 | ||
camelia | 4.200000e+01 | ||
stevied | do you know a good way to do pretty scientific notation. so instead of 5e-06 you'd have 5 x 10-6 (with the -6 in superscript)? | 12:57 | |
lizmat | no way to do that in core, I don't think | 12:59 | |
m: dd "x".uninames | |||
camelia | ("LATIN SMALL LETTER X",).Seq | ||
lizmat | I guess you could whip up an elaborate .subst to do that transformation | 13:00 | |
stevied | sounds like a good idea | 13:01 | |
ok, so let's say I want to convert the string `06` to a superscript. what's a good way to do that? | 13:16 | ||
the zero would be dropped | |||
lizmat | gist.github.com/lizmat/42d59853c1d...79b7043efc | 13:19 | |
stevied | ah, trans, good idea | 13:20 | |
here's what I came up with, FWIW: | 13:28 | ||
``` | |||
if ($num < 10000) { | |||
my $exp = $num ~~ /\d+$/; | |||
$exp = $exp.subst(/^0/, ''); | |||
$exp .= trans( [0..9] => [ "\x[2070]".."\x[2079]" ] ); | |||
$num = $num.subst(/e\-\d+/, " x 10\x[207B]$exp"); | |||
} | |||
``` | |||
thanks, Liz! | |||
13:40
Oshawott joined
13:43
archenoth left
13:44
lizmat left
|
|||
Anton Antonov | Which are packages I can assume are always found in Raku? | 15:01 | |
Well, I do not need to know. š I figured to get what I want without that assumption... | 15:20 | ||
16:17
Oshawott left
16:36
kueppo joined
16:51
kueppo left
19:36
lizmat joined
19:45
lizmat left
|
|||
stevied | I have these two classes: | 20:01 | |
``` | |||
class Unit-ug is Unit { | |||
method new(Rat:D() $value = 1.0) { | |||
self.bless( | |||
:UnitType('Mass'), | |||
:abbr('Āµg'), | |||
:base_value(1/1_000_000), | |||
:$value, | |||
:si, | |||
); | |||
} | |||
} | |||
class Unit-mcg is Unit-ug { } | |||
I want the Unit-mcg to have a different abbreviation than Unit-ug. Everything else is the same. What's the cleanest way to do that? | 20:03 | ||
best I came up with is this: | 20:28 | ||
``` | |||
class Mass-ug is Mass { | |||
method new(Rat:D() $value = 1.0) { | |||
self.bless( | |||
:abbr('Āµg'), | |||
:base_value(1/1_000_000), | |||
:$value, | |||
:si, | |||
); | |||
} | |||
} | |||
wamba | > class Mass-mcg is Mass-ug { | 21:11 | |
> method TWEAK { self.clone( abbr => "mcg" ) } | |||
> }; | |||
or | |||
> class Mass-mcg is Mass-ug { | |||
> method TWEAK { $.abbr = "mcg" } | |||
> }; | |||
> if `abbr` us mutable | |||
But, why do you define your own `new`? | 21:13 | ||
or | |||
> class Mass-mcg is Mass-ug { | |||
> method TWEAK { $.abbr = "mcg" } | |||
> }; | |||
if `abbr` us mutable | |||
or | |||
> class Mass-mcg is Mass-ug { | |||
> method TWEAK { $.abbr = "mcg" } | |||
> }; | |||
if `abbr` is mutable | |||
stevied | ah, nice. forgot about TWEAK. | 21:17 | |
not sure what you mean. but Mass-ug is subclass of Mass class: | 21:19 | ||
which looks like this: | 21:21 | ||
``` | |||
class Mass { | |||
has Str $!name; | |||
has Str $.abbr is rw; | |||
21:21
discord-raku-bot left
21:22
discord-raku-bot joined
|
|||
class Mass-lb is Mass { | 21:34 | ||
method new(Rat:D() $value = 1.0) { | |||
self.bless( | |||
:abbr('lb'), | |||
:base_value(453.59237), | |||
:$value, | |||
); | |||
} | |||
} | |||
my @lb-syn = < lbs pounds pound >; | |||
for @lb-syn { | |||
EVAL 'class ::("Mass-$_") is Mass-lb {}'; | |||
Wondering if there is a way to easily generatate aliases for classes. Tried this: | |||
``` | |||
class Mass-lb is Mass { | |||
method new(Rat:D() $value = 1.0) { | |||
self.bless( | |||
:abbr('lb'), | |||
:base_value(453.59237), | |||
:$value, | |||
); | |||
} | |||
} | |||
my @lb-syn = < lbs pounds pound >; | |||
for @lb-syn { | |||
EVAL 'class ::("Mass-$_") is Mass-lb {}'; | |||
but it throws an error: | 21:36 | ||
`Name ::("Mass-$_") is not compile-time known, and can not serve as a package name` |