Re: A stricter typed variable
Hi, Fernando Santagata said: I my case, what I was trying to do is getting some strings in order to pass them over to a C function via NativeCall. Being sure the passed value was actually a string is not a whim, but would be useful in avoiding a segmentation fault. Yet I figure that as long as anyone behaves it will all run smoothly enough and I understand that using Perl6 to interface one's program to a C library is not the best use case for a high level functional language ;-) Does this work for you? sub test(Str :$format, Str :@filter) { say $format.WHAT; say @filter.WHAT; } my Str @f = ("a", "b", "1"); test format => "gnutar", filter => @f; - Salve -- #!/usr/bin/env perl sub AUTOLOAD{$AUTOLOAD=~/.*::(\d+)/;seek(DATA,$1,0);print# Salve Joshua Nilsen getc DATA}$"="'};&{'";@_=unpack("C*",unpack("u*",':50,$'.# '3!=0"59,6!`%%P\0!1)46%!F.Q`%01,`'."\n"));eval "&{'@_'}"; __END__ is near! :)
Re: A stricter typed variable
Note that you can also use subset to create a type: subset ArrayOfStr of Array where .all ~~ Str; sub foo (@a where ArrayOfStr) {...} or: sub foo (ArrayOfStr $a) {...} On 2017-01-08 10:33:36 GMT, Fernando Santagata wrote: > Thank you! > > On Sat, Jan 7, 2017 at 5:23 PM, Siavash > wrote: > >> >> Hi, >> >> If you really want to avoid `Array[Str].new`, you can do something like >> this: >> >> sub foo (@a where .all ~~ Str) {...} >> >> On 2017-01-07 11:45:55 GMT, Fernando Santagata wrote: >> > Hello, >> > >> > I have a function like this: >> > >> > sub test(Str :$format, :@filter) >> > { >> > say $format; >> > say @filter; >> > } >> > >> > and I wish to have a stricter type control over the second parameter. >> > >> > The natural way to do it seemed this: >> > >> > sub test(Str :$format, Array[Str] :$filter) >> > { >> > say $format; >> > say $filter; >> > } >> > >> > but then I have to call it this way: >> > >> > test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); >> > >> > Is there a less cumbersome way to do it? >> > >> > Obviously this doesn't work: >> > >> > test format => 'gnutar', filter => ; >> > >> > because the argument is interpreted as a generic List. >> > This doesn't work either: >> > >> > test format => 'gnutar', filter => ['gzip', 'uuencode']; >> > >> > because it's interpreted as a generic Array, not an Array[Str]. >> > >> > Thank you! >> >>
Re: A stricter typed variable
Thank you! On Sat, Jan 7, 2017 at 5:23 PM, Siavash wrote: > > Hi, > > If you really want to avoid `Array[Str].new`, you can do something like > this: > > sub foo (@a where .all ~~ Str) {...} > > On 2017-01-07 11:45:55 GMT, Fernando Santagata wrote: > > Hello, > > > > I have a function like this: > > > > sub test(Str :$format, :@filter) > > { > > say $format; > > say @filter; > > } > > > > and I wish to have a stricter type control over the second parameter. > > > > The natural way to do it seemed this: > > > > sub test(Str :$format, Array[Str] :$filter) > > { > > say $format; > > say $filter; > > } > > > > but then I have to call it this way: > > > > test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); > > > > Is there a less cumbersome way to do it? > > > > Obviously this doesn't work: > > > > test format => 'gnutar', filter => ; > > > > because the argument is interpreted as a generic List. > > This doesn't work either: > > > > test format => 'gnutar', filter => ['gzip', 'uuencode']; > > > > because it's interpreted as a generic Array, not an Array[Str]. > > > > Thank you! > > -- Fernando Santagata
Re: A stricter typed variable
On 1/7/17, Moritz Lenz wrote: > I observe some weird behavior in Perl 6 newbies (and I've observed it in > myself too): they're so enamored by type constraints that they use them > everywhere, and run into all sorts of type errors they didn't expect. > There seems to be no feature in software (especially languages), so clearly defined and explained, that it won't immediately be misunderstood and hammered into an inappropriate use, (in some cases almost contradicting the original concept).
Re: A stricter typed variable
Hi, If you really want to avoid `Array[Str].new`, you can do something like this: sub foo (@a where .all ~~ Str) {...} On 2017-01-07 11:45:55 GMT, Fernando Santagata wrote: > Hello, > > I have a function like this: > > sub test(Str :$format, :@filter) > { > say $format; > say @filter; > } > > and I wish to have a stricter type control over the second parameter. > > The natural way to do it seemed this: > > sub test(Str :$format, Array[Str] :$filter) > { > say $format; > say $filter; > } > > but then I have to call it this way: > > test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); > > Is there a less cumbersome way to do it? > > Obviously this doesn't work: > > test format => 'gnutar', filter => ; > > because the argument is interpreted as a generic List. > This doesn't work either: > > test format => 'gnutar', filter => ['gzip', 'uuencode']; > > because it's interpreted as a generic Array, not an Array[Str]. > > Thank you!
Re: A stricter typed variable
On Sat, Jan 7, 2017 at 4:19 PM, Moritz Lenz wrote: > > How is this an > > "exaggerated" use of containers? Why have the language feature at all if > > it's too clunky for people to use it? > > I'm not saying it's always too clunky to use. I'm saying there's a cost > attached, and you should consider whether to use it. > > I observe some weird behavior in Perl 6 newbies (and I've observed it in > myself too): they're so enamored by type constraints that they use them > everywhere, and run into all sorts of type errors they didn't expect. > Thanks for replying! I my case, what I was trying to do is getting some strings in order to pass them over to a C function via NativeCall. Being sure the passed value was actually a string is not a whim, but would be useful in avoiding a segmentation fault. Yet I figure that as long as anyone behaves it will all run smoothly enough and I understand that using Perl6 to interface one's program to a C library is not the best use case for a high level functional language ;-) -- Fernando Santagata
Re: A stricter typed variable
Hi, On 07.01.2017 15:52, Joseph Garvin wrote: > Being able to type the elements in containers was considered a major > problem in Java for years before they added generics. Please note that Java and Perl 6 are coming from very different directions. Java 1 a statically typed language, where every operation whose type safety the compiler can't prove at compile time is rejected. So with untyped containers in Java 1.4-, you needed explicit runtime casts juts to call methods on container elements. Perl used to be a dynamically typed language, which means that the compiler basically never rejected a program due to type errors. Type errors can happen at run time. > And the whole > point of having a computer is to have it do repetitive things, e.g. loop > over a bunch of stuff and do the same thing to all of it. You can loop over a bunch of stuff and do the same thing to all of it without explicitly typing the contents of an array. That's what all other dynamically typed languages tend to do (python, ruby, lua, PHP, you name it). If you have, for example: sub wants-str(Str $x) { ...} sub test(@a) { for @a -> $x { wants-str($x) } } Perl 6 happily compiles and executes this, because it can't prove a type error at compile time. Java OTOH would reject the equivalent code. > How is this an > "exaggerated" use of containers? Why have the language feature at all if > it's too clunky for people to use it? I'm not saying it's always too clunky to use. I'm saying there's a cost attached, and you should consider whether to use it. I observe some weird behavior in Perl 6 newbies (and I've observed it in myself too): they're so enamored by type constraints that they use them everywhere, and run into all sorts of type errors they didn't expect. The weird part is that if they wanted a statically typed language, they could have easily chosen one. But they picked Perl 6, and then they try to use it like a statically language, and wonder why it feels clunky. If you write a type constraint, ask yourself: could there be an object from another type that my could would also work with? Maybe I'm just calling a method that's implemented in type Str, but a user-supplied object could implement the same method as well. If it walks like a duck, and quacks like a duck, do I really have to care if it's a duck or not? Cheers, Moritz -- Moritz Lenz https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/
Re: A stricter typed variable
Being able to type the elements in containers was considered a major problem in Java for years before they added generics. And the whole point of having a computer is to have it do repetitive things, e.g. loop over a bunch of stuff and do the same thing to all of it. How is this an "exaggerated" use of containers? Why have the language feature at all if it's too clunky for people to use it? On Jan 7, 2017 8:42 AM, "Moritz Lenz" wrote: > Hi, > > On 07.01.2017 12:45, Fernando Santagata wrote: > > Hello, > > > > I have a function like this: > > > > sub test(Str :$format, :@filter) > > { > > say $format; > > say @filter; > > } > > > > and I wish to have a stricter type control over the second parameter. > > > > The natural way to do it seemed this: > > > > sub test(Str :$format, Array[Str] :$filter) > > { > > say $format; > > say $filter; > > } > > > > but then I have to call it this way: > > > > test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); > > > > Is there a less cumbersome way to do it? > > As you have discovered, you need to be explicit about the type of the > argument that's passed in. > > You can use anonymous variables to write an Array[Str] more compact: > > test format => 'gnutar', filter => my Str @ = 'gzip', 'uuencode'; > > or even > > test format => 'gnutar', filter => my Str @ = ; > > My general advise is to not exaggerate the usage of container types. > Think of Perl 6 as a dynamically typed language, and only add types > where they make life easier for everybody involved. > > Cheers, > Moritz > > -- > Moritz Lenz > https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/ >
Re: A stricter typed variable
Hi, On 07.01.2017 12:45, Fernando Santagata wrote: > Hello, > > I have a function like this: > > sub test(Str :$format, :@filter) > { > say $format; > say @filter; > } > > and I wish to have a stricter type control over the second parameter. > > The natural way to do it seemed this: > > sub test(Str :$format, Array[Str] :$filter) > { > say $format; > say $filter; > } > > but then I have to call it this way: > > test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); > > Is there a less cumbersome way to do it? As you have discovered, you need to be explicit about the type of the argument that's passed in. You can use anonymous variables to write an Array[Str] more compact: test format => 'gnutar', filter => my Str @ = 'gzip', 'uuencode'; or even test format => 'gnutar', filter => my Str @ = ; My general advise is to not exaggerate the usage of container types. Think of Perl 6 as a dynamically typed language, and only add types where they make life easier for everybody involved. Cheers, Moritz -- Moritz Lenz https://deploybook.com/ -- https://perlgeek.de/ -- https://perl6.org/
A stricter typed variable
Hello, I have a function like this: sub test(Str :$format, :@filter) { say $format; say @filter; } and I wish to have a stricter type control over the second parameter. The natural way to do it seemed this: sub test(Str :$format, Array[Str] :$filter) { say $format; say $filter; } but then I have to call it this way: test format => 'gnutar', filter => Array[Str].new('gzip', 'uuencode'); Is there a less cumbersome way to do it? Obviously this doesn't work: test format => 'gnutar', filter => ; because the argument is interpreted as a generic List. This doesn't work either: test format => 'gnutar', filter => ['gzip', 'uuencode']; because it's interpreted as a generic Array, not an Array[Str]. Thank you! -- Fernando Santagata