Re: [go-nuts] how to write methods for a generic Matrix[float64 constrained by Addable]?

2023-09-09 Thread 'Jakob Borg' via golang-nuts
Two minor issues; you can’t declare func (m *Matrix[float64]) NanToZero() 
because we don’t have specialisation, it needs to be func (m *Matrix[T]) 
NanToZero(), and you can’t have complex as one of the possible types because 
it’s impossible to convert a complex to a float.

https://go.dev/play/p/QnWoKJ4bRcI

//jb

On 10 Sep 2023, at 01:31, Jason E. Aten  wrote:

I made a playground if anyone wants to play with it and figure out how to test 
their ideas.

https://go.dev/play/p/h1nqHJAdVG8

On Sunday, September 10, 2023 at 12:24:54 AM UTC+1 Jason E. Aten wrote:
Like how, specifically?

func (m *Matrix[float64]) NanToZero() {
for i := 0; i < m.Nrow; i++ {
for j := 0; j < m.Ncol; j++ {
v := m.At(i, j).(float64)
if math.IsNaN(v) {
m.Set(i, j, 0)
}
}
}
}
gives
./slurp1.go:944:9: invalid operation: cannot use type assertion on type 
parameter value m.At(i, j) (value of type float64 constrained by Addable)

On Sunday, September 10, 2023 at 12:04:53 AM UTC+1 Dan Kortschak wrote:
On Sat, 2023-09-09 at 15:39 -0700, Jason E. Aten wrote:
> New to generics... how can I write a method for a Matrix[float64]
> that uses a math function that expects a float64?   I get:
>
> ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> constrained by Addable) as float64 value in argument to math.IsNaN
>
> Casting it to float64() does not change this error(!)
>
> here is the essentials:
>
> type Addable interface {
> ~complex128 | ~complex64 | ~float64 | ~float32 |
> ~byte | ~uint16 | ~uint32 | ~uint64 |
> ~int8 | ~int16 | ~int32 | ~int64 | ~int
> }
>
> type Matrix[T Addable] struct {
> Nrow int
> Ncol int
> Dat[]T
> }
> ...
> func (m *Matrix[float64]) NanToZero() {
> for i := 0; i < m.Nrow; i++ {
> for j := 0; j < m.Ncol; j++ {
>
> if math.IsNaN(m.At(i, j)) {  // line 936, where the error
> is; ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> constrained by Addable) as float64 value in argument to math.IsNaN
>
> m.Set(i, j, 0)
> }
> }
> }
> }
>
> go version go1.20.6 linux/amd64

You will probably need to type assert.


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to 
golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4f7e642b-ef44-4564-93e7-b2f7c10ffb6dn%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/DB33082D-B560-4C0C-846E-1E23F8BF846E%40kastelo.net.


[go-nuts] Generic function for types with common method name but different return type

2023-09-09 Thread Oleh Lomaka
Hello everybody,

Gophers' superpower is required and highly appreciated.

I have two types, A and B, each of which has a method named "M". This 
method accepts a string and returns an instance of the type it is declared 
on.

```go
type A struct {}
func (a A) M (string) A {
return a
}
type B struct {}
func (b B) M (string) B {
   return b
}
```
What's the best way to declare a generic function that receives an instance 
of one of these types along with a string? Depending on the string, this 
function should either return the instance it received or call method M on 
it and return the instance produced by this method.

Currently, I've come up with two solutions, but I'm not entirely satisfied 
with either:

https://go.dev/play/p/h5jzISRyTkF In this approach, I'm not fond of how I 
must call the f function on line 30. Without specifying a type parameter, 
the call fails to compile. Additionally, I'm concerned about potential 
panics on line 23. If I expand the code with new types in the future, 
there's a risk of type assertion failures.

https://go.dev/play/p/rHHjV_A-hvK While this solution appears cleaner, it's 
prone to panicking if I introduce another type with an incompatible M 
signature (or without M method completely)

Maybe someone has better ideas.

Thank you.
All the best,

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ef5d6858-d1f6-4cb2-8875-94369878a77cn%40googlegroups.com.


Re: [go-nuts] how to write methods for a generic Matrix[float64 constrained by Addable]?

2023-09-09 Thread Jason E. Aten
I made a playground if anyone wants to play with it and figure out how to 
test their ideas.

https://go.dev/play/p/h1nqHJAdVG8

On Sunday, September 10, 2023 at 12:24:54 AM UTC+1 Jason E. Aten wrote:

> Like how, specifically?
>
> func (m *Matrix[float64]) NanToZero() {
> for i := 0; i < m.Nrow; i++ {
> for j := 0; j < m.Ncol; j++ {
> v := m.At(i, j).(float64)
> if math.IsNaN(v) {
> m.Set(i, j, 0)
> }
> }
> }
> }
> gives
> ./slurp1.go:944:9: invalid operation: cannot use type assertion on type 
> parameter value m.At(i, j) (value of type float64 constrained by Addable)
>
> On Sunday, September 10, 2023 at 12:04:53 AM UTC+1 Dan Kortschak wrote:
>
>> On Sat, 2023-09-09 at 15:39 -0700, Jason E. Aten wrote: 
>> > New to generics... how can I write a method for a Matrix[float64] 
>> > that uses a math function that expects a float64?   I get: 
>> > 
>> > ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
>> > constrained by Addable) as float64 value in argument to math.IsNaN 
>> > 
>> > Casting it to float64() does not change this error(!) 
>> > 
>> > here is the essentials: 
>> > 
>> > type Addable interface { 
>> > ~complex128 | ~complex64 | ~float64 | ~float32 | 
>> > ~byte | ~uint16 | ~uint32 | ~uint64 | 
>> > ~int8 | ~int16 | ~int32 | ~int64 | ~int 
>> > } 
>> > 
>> > type Matrix[T Addable] struct { 
>> > Nrow int 
>> > Ncol int 
>> > Dat[]T 
>> > } 
>> > ... 
>> > func (m *Matrix[float64]) NanToZero() { 
>> > for i := 0; i < m.Nrow; i++ { 
>> > for j := 0; j < m.Ncol; j++ { 
>> > 
>> > if math.IsNaN(m.At(i, j)) {  // line 936, where the error 
>> > is; ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
>> > constrained by Addable) as float64 value in argument to math.IsNaN 
>> > 
>> > m.Set(i, j, 0) 
>> > } 
>> > } 
>> > } 
>> > } 
>> > 
>> > go version go1.20.6 linux/amd64 
>>
>> You will probably need to type assert. 
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4f7e642b-ef44-4564-93e7-b2f7c10ffb6dn%40googlegroups.com.


Re: [go-nuts] how to write methods for a generic Matrix[float64 constrained by Addable]?

2023-09-09 Thread Jason E. Aten
Like how, specifically?

func (m *Matrix[float64]) NanToZero() {
for i := 0; i < m.Nrow; i++ {
for j := 0; j < m.Ncol; j++ {
v := m.At(i, j).(float64)
if math.IsNaN(v) {
m.Set(i, j, 0)
}
}
}
}
gives
./slurp1.go:944:9: invalid operation: cannot use type assertion on type 
parameter value m.At(i, j) (value of type float64 constrained by Addable)

On Sunday, September 10, 2023 at 12:04:53 AM UTC+1 Dan Kortschak wrote:

> On Sat, 2023-09-09 at 15:39 -0700, Jason E. Aten wrote:
> > New to generics... how can I write a method for a Matrix[float64]
> > that uses a math function that expects a float64?   I get:
> > 
> > ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> > constrained by Addable) as float64 value in argument to math.IsNaN
> > 
> > Casting it to float64() does not change this error(!)
> > 
> > here is the essentials:
> > 
> > type Addable interface {
> > ~complex128 | ~complex64 | ~float64 | ~float32 |
> > ~byte | ~uint16 | ~uint32 | ~uint64 |
> > ~int8 | ~int16 | ~int32 | ~int64 | ~int
> > }
> > 
> > type Matrix[T Addable] struct {
> > Nrow int
> > Ncol int
> > Dat[]T
> > }
> > ...
> > func (m *Matrix[float64]) NanToZero() {
> > for i := 0; i < m.Nrow; i++ {
> > for j := 0; j < m.Ncol; j++ {
> > 
> > if math.IsNaN(m.At(i, j)) {  // line 936, where the error
> > is; ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> > constrained by Addable) as float64 value in argument to math.IsNaN
> > 
> > m.Set(i, j, 0)
> > }
> > }
> > }
> > }
> > 
> > go version go1.20.6 linux/amd64
>
> You will probably need to type assert.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5674028a-c474-4110-8467-2b29d89ccdcbn%40googlegroups.com.


Re: [go-nuts] how to write methods for a generic Matrix[float64 constrained by Addable]?

2023-09-09 Thread 'Dan Kortschak' via golang-nuts
On Sat, 2023-09-09 at 15:39 -0700, Jason E. Aten wrote:
> New to generics... how can I write a method for a Matrix[float64]
> that uses a math function that expects a float64?   I get:
> 
> ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> constrained by Addable) as float64 value in argument to math.IsNaN
> 
> Casting it to float64() does not change this error(!)
> 
> here is the essentials:
> 
> type Addable interface {
>     ~complex128 | ~complex64 | ~float64 | ~float32 |
>         ~byte | ~uint16 | ~uint32 | ~uint64 |
>         ~int8 | ~int16 | ~int32 | ~int64 | ~int
> }
> 
> type Matrix[T Addable] struct {
>     Nrow int
>     Ncol int
>     Dat        []T
> }
> ...
> func (m *Matrix[float64]) NanToZero() {
>     for i := 0; i < m.Nrow; i++ {
>         for j := 0; j < m.Ncol; j++ {
> 
>             if math.IsNaN(m.At(i, j)) {  // line 936, where the error
> is; ./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64
> constrained by Addable) as float64 value in argument to math.IsNaN
> 
>                 m.Set(i, j, 0)
>             }
>         }
>     }
> }
> 
> go version go1.20.6 linux/amd64

You will probably need to type assert.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/666e12c82257dd5dc9e207767061eaa65d2ffa13.camel%40kortschak.io.


[go-nuts] how to write methods for a generic Matrix[float64 constrained by Addable]?

2023-09-09 Thread Jason E. Aten
New to generics... how can I write a method for a Matrix[float64] that uses 
a math function that expects a float64?   I get:

./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
constrained by Addable) as float64 value in argument to math.IsNaN

Casting it to float64() does not change this error(!)

here is the essentials:

type Addable interface {
~complex128 | ~complex64 | ~float64 | ~float32 |
~byte | ~uint16 | ~uint32 | ~uint64 |
~int8 | ~int16 | ~int32 | ~int64 | ~int
}

type Matrix[T Addable] struct {
Nrow int
Ncol int
Dat[]T
}
...
func (m *Matrix[float64]) NanToZero() {
for i := 0; i < m.Nrow; i++ {
for j := 0; j < m.Ncol; j++ {

if math.IsNaN(m.At(i, j)) {  // line 936, where the error is; 
./slurp1.go:936:18: cannot use m.At(i, j) (value of type float64 
constrained by Addable) as float64 value in argument to math.IsNaN

m.Set(i, j, 0)
}
}
}
}

go version go1.20.6 linux/amd64

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ec30b658-112c-488d-9e9d-56143f79455an%40googlegroups.com.


Re: [go-nuts] x/mobile: avoid to catch sigabort signal

2023-09-09 Thread TheDiveO
You seem to be barking up the wrong tree, at least for the moment. Maybe 
your team may want to learn more about cgo and by sheer coincidence there's 
a blog post  https://shane.ai/posts/cgo-performance-in-go1.21/ that might 
serve as a starting point, with one or two pointers to go further.  I also 
don't understand why you are so keen on the SIGABRT catch, when there is 
probably a much deeper problem in your code or the libs you use, s others 
already told you.  Trying to hide the signal under the carpet isn't the 
best strategy to my limited understanding, as your app is already in such a 
bad state that the runtime has to fail.   
On Friday, September 8, 2023 at 11:38:38 PM UTC+2 Danilo bestbug wrote:

> Ehy Ian, thanks for the response. Apology if I was not clear, I try to 
> explain in a different way but it's hard for me too since I'm not 100% 
> confident about what it's happening here exactly, I'm tring to follow the 
> trace but any feedback is more than welcome.
> The problem I'm facing is the following: I've a small utility written in 
> GO and integrated in an app iOS as SDK. At the moment if I've undestood 
> correctly from the thread I've linked the GO runtime are cacthing a 
> sigabort signal that are not from the GO stack but it's generated from 
> another thread with the result that it's look like the GO runtime is 
> crashing from the apple report.
>
> If this behavior of the GO runtime is legittime when GO is an executable 
> in my context is a problem since the developer follow the GO stack instead 
> of the other thread stack.
>
> Now what I'm try to understand if this behavior can be somehow change, and 
> if so how should I do?
> Il giorno venerdì 8 settembre 2023 alle 22:34:07 UTC+2 Ian Lance Taylor ha 
> scritto:
>
>> On Thu, Sep 7, 2023 at 11:41 PM Danilo bestbug 
>>  wrote: 
>> > 
>> > Some weeks ago I've opened a possible bug on github and the only 
>> response I received is a reference to 
>> > "This looks like the program (the Go runtime, or not) intentionally 
>> crashing when it is already in a bad condition, like receiving an unhandled 
>> signal on a non-Go thread." 
>> > 
>> > I would like to stop the GO system to do this kind of behaviour 
>> (intercepting unhandled signal) otherwise the team who work on the crash 
>> keep searching the problem on the GO thread crashed instead of elsewhere. 
>> This for us is a big problem and I love if someone can help me to address 
>> this matter! 
>>
>> I'm sorry, I don't really understand what you are asking. What I can 
>> tell you is that signal handling in Go programs is managed via the 
>> os/signal package. See https://pkg.go.dev/os/signal. 
>>
>> Ian 
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4305ee4c-664a-43ff-b1d8-8e0439f74b91n%40googlegroups.com.