简体   繁体   English

当与 C 接口时,如何在 lostanza 中表示 NULL 指针?

[英]How to represent NULL pointer in lostanza when interfaces with C?

This code works perfectly (ignore the color information part for now)此代码完美运行(暂时忽略颜色信息部分)

lostanza deftype SDL_Rect :
   x: int
   y: int
   w: int
   h: int

lostanza defn SDL_Rect (x:ref<Int>, y:ref<Int>, w:ref<Int>, h:ref<Int>) -> ref<SDL_Rect> :
   return new SDL_Rect{x.value, y.value, w.value, h.value}

extern SDL_FillRect: (long, ptr<SDL_Rect>, int) -> int

lostanza defn call-SDL_FillRect (p : ref<Long>, rect : ref<SDL_Rect>) -> ref<Int> :
   val pRect = addr!([rect])
   val result = call-c SDL_FillRect(p.value, pRect, 0x008000ff)
   return new Int{result}
val rect = SDL_Rect(50,50,50,50)
val fr = call-SDL_FillRect(ws, rect)

However, SDL_FillRect in C accepts a NULL pointer in pRect.但是C中的SDL_FillRect在pRect中接受了一个NULL的指针。 I suppose I can do this我想我可以做到

lostanza defn call-SDL_FillRect (p : ref<Long>, rect : ref<SDL_Rect|False>) -> ref<Int> :
   var pRect = 0   ; it does not work even using "0 as ptr<SDL_Rect>"
   match(rect) :
      (rect : ref<SDL_Rect>) : pRect = addr!([rect])
      
   val pRect = addr!([rect])
   val result = call-c SDL_FillRect(p.value, pRect, 0x008000ff)
   return new Int{result}

But it does not work.但它不起作用。 What is the practice to force a NULL pointer in lostanza?在 lostanza 中强制使用 NULL 指针的做法是什么?

I have I have figured it out:我已经弄明白了:

Using 0L as a NULL pointer使用 0L 作为 NULL 指针

lostanza defn call-SDL_FillRect (p : ref<Long>, rect : ref<SDL_Rect|False>, argb : ref<Int>) -> ref<Int> :
   var pRect : ptr<SDL_Rect>
   match(rect) :
      (rect : ref<SDL_Rect>) : pRect = addr!([rect])
      (rect) : pRect = 0L as ptr<SDL_Rect>
   val result = call-c SDL_FillRect(p.value, pRect, argb.value)
   return new Int{result}

There is also the null value.还有null值。 It is defined in core as you have found:正如您所发现的,它在core中定义:

public lostanza val null : ptr<?> = 0L as ptr<?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM