~sircmpwn/hare-users

9 3

replace type assertion with match?

Details
Message ID
<82d5384a-e2dc-4def-8a1a-657ee10d919c@app.fastmail.com>
DKIM signature
pass
Download raw message
In the tutorial, in the example with bufio::scanline
https://harelang.org/tutorials/introduction/#a-closer-look-at-bufioscanline ,
there's a type assertion (`as` operator) after the call to bufio::scanline.

Is this type assertion an alternative to using a `match` and trying to
match against the io::EOF return val?

How could I modify that sample code to match on the return value of
bufio::scanline? Here's my attempt:

~~~hare
use bufio;
use fmt;
use os;
use io;
use strings;

export fn main() void = {
	const user = askname();
	greet(user);
};

// Asks the user to provide their name.
fn askname() str = {
	fmt::println("Hello! Please enter your name:")!;
	const name = match (bufio::scanline(os::stdin)!) {
		case let name: []u8 =>
			yield name;
		case let eof: io::EOF =>
			fmt::println("reached eof with no input")!;
	};
	return strings::fromutf8(name)!;
};

// Greets a user by name.
fn greet(user: str) void = {
	fmt::printfln("Hello, {}!", user)!;
};
~~~

but that fails on line 17 ("case let eof: io::EOF") with, "Cannot create binding for type of zero or undefined size".
Details
Message ID
<CSM7V0M4RT2S.J2LNQL2CAF3O@monch>
In-Reply-To
<82d5384a-e2dc-4def-8a1a-657ee10d919c@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
match (bufio::scanline(os::stdin)) {
case io::EOF =>
	fmt::println("reached eof with no input")!;
case let name: []u8 =>
	yield name;
};

there's a ticket for allowing zero-sized bindings, but there's also not
really a usecase for them in this context
Details
Message ID
<f0f44a3d-c59d-4f5e-9a97-f2cfb30d2e48@app.fastmail.com>
In-Reply-To
<CSM7V0M4RT2S.J2LNQL2CAF3O@monch> (view parent)
DKIM signature
pass
Download raw message
On Sun, May 14, 2023, at 2:36 PM, Ember Sawady wrote:
> match (bufio::scanline(os::stdin)) {
> case io::EOF =>
> 	fmt::println("reached eof with no input")!;
> case let name: []u8 =>
> 	yield name;
> };
>
> there's a ticket for allowing zero-sized bindings, but there's also not
> really a usecase for them in this context

Ah, because io::EOF is of type void, this would make it a zero-sized
binding. (Also, I see that there's no additional info about an EOF that
you'd need.)

If the alternative return type (in this case, an io::EOF (the alternative to
[]u8)) were instead a non-zero-sized binding, am I correct in thinking that
the type assertion would be the less-careful alternative to using a full
match/case expression?
Details
Message ID
<72c80cc0-8ade-4cd6-9816-29fbf4004d39@app.fastmail.com>
In-Reply-To
<f0f44a3d-c59d-4f5e-9a97-f2cfb30d2e48@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
On Sun, May 14, 2023, at 3:04 PM, John Gabriele wrote:
> On Sun, May 14, 2023, at 2:36 PM, Ember Sawady wrote:
>> match (bufio::scanline(os::stdin)) {
>> case io::EOF =>
>> 	fmt::println("reached eof with no input")!;
>> case let name: []u8 =>
>> 	yield name;
>> };
>>
>> there's a ticket for allowing zero-sized bindings, but there's also not
>> really a usecase for them in this context
>
> Ah, because io::EOF is of type void, this would make it a zero-sized
> binding. (Also, I see that there's no additional info about an EOF that
> you'd need.)
>
> If the alternative return type (in this case, an io::EOF (the alternative to
> []u8)) were instead a non-zero-sized binding, am I correct in thinking that
> the type assertion would be the less-careful alternative to using a full
> match/case expression?

Oh! I see that I don't need to assign in a `case` --- I can just do
`case some::type =>` (rather than `case let foo: some::type =>`).
In that case (no pun intended), I see that this works:

~~~
use bufio;
use fmt;
use os;
use io;
use strings;

export fn main() void = {
	const user = askname();
	greet(user);
};

// Asks the user to provide their name.
fn askname() str = {
	fmt::println("Hello! Please enter your name:")!;
	const name = match (bufio::scanline(os::stdin)!) {
	case let name: []u8 =>
		yield name;
	case io::EOF =>
		fmt::println("reached eof with no input")!;
		return "No-name";
	};
	return strings::fromutf8(name)!;
};

// Greets a user by name.
fn greet(user: str) void = {
	fmt::printfln("Hello, {}!", user)!;
};
~~~
Details
Message ID
<e02b9b10-172c-3b40-59ad-f19e21c28be0@mykolab.ch>
In-Reply-To
<CSM7V0M4RT2S.J2LNQL2CAF3O@monch> (view parent)
DKIM signature
pass
Download raw message
I'm sorry for adding to an older thread, but I'm going through the same 
process, and having read these posts, I'm still not getting the following:

This is in the tutorial:

---

const num = match (bufio::scanline(os::stdin)?) {
case io::EOF =>
	return unexpectedeof;
case let buf: []u8 =>
	yield strings::fromutf8(buf)!;
};

---

Why doesn't the following work then? (It gives "Cannot create binding for type of zero or undefined size"):

---
fn someF() void = {
	let m:(i32|u32) = 2;

	let w = match (m) {
	case let v:u32 => {
		yield v;
	};
	case let v:i32 => {
		yield v;
	};
	};
};
---

Adding a type hint, like `let w:(i32|u32)`, results in "Initializer is not assignable to binding type".

Thank you

Mike
Details
Message ID
<CUMHZPLN92VF.1SBYNB6P96Q7T@eiger>
In-Reply-To
<e02b9b10-172c-3b40-59ad-f19e21c28be0@mykolab.ch> (view parent)
DKIM signature
pass
Download raw message
On Mon Aug 7, 2023 at 4:59 PM UTC, Mike Eichler wrote:
> fn someF() void = {
> 	let m:(i32|u32) = 2;
>
> 	let w = match (m) {
> 	case let v:u32 => {
> 		yield v;
> 	};
> 	case let v:i32 => {
> 		yield v;
> 	};
> 	};
> };

there's an implicit compound expression in switch/match bodies, so the
result type of that match is void. getting rid of the {} should fix it

also note that, per the style guide, there should be a space between the
`:` and the type
Details
Message ID
<83b6d2dc-01a7-8d56-75d3-d4cab83be351@mykolab.ch>
In-Reply-To
<CSM7V0M4RT2S.J2LNQL2CAF3O@monch> (view parent)
DKIM signature
pass
Download raw message
Thank you very much for explaining this to me - removing the curly 
braces works!

Noted about the space between the ":" and the type.
Details
Message ID
<4db47c88-b938-487c-9bf8-1f0a197e7fae@app.fastmail.com>
In-Reply-To
<83b6d2dc-01a7-8d56-75d3-d4cab83be351@mykolab.ch> (view parent)
DKIM signature
pass
Download raw message
Looking back at the examples I posted, I see that I kept accidentally
writing `match (bufio::scanline(os::stdin)!) { ...`  (with the `!` after the
function call that I'm matching on). Seems like this would be at odds with
(or defeat the purpose of) the rest of the match body, no? If that's the
case, should the compiler issue a warning about it?


On Mon, Aug 7, 2023, at 2:02 PM, Mike Eichler wrote:
> Thank you very much for explaining this to me - removing the curly 
> braces works!
>
> Noted about the space between the ":" and the type.
>
>
> Attachments:
> * OpenPGP_0x9E4F59675CED52AA.asc
> * OpenPGP_signature
Details
Message ID
<CUQX264TY9A0.15PUIDVEUGHQD@monch>
In-Reply-To
<4db47c88-b938-487c-9bf8-1f0a197e7fae@app.fastmail.com> (view parent)
DKIM signature
pass
Download raw message
mind updating my name in your contacts? i think fastmail must've been a
bit overzealous in remembering stuff during some brief interactions we
had on hare-dev a bit over a year ago and it's kinda distracting lol

On Sat Aug 12, 2023 at 10:11 PM UTC, John Gabriele wrote:
> Looking back at the examples I posted, I see that I kept accidentally
> writing `match (bufio::scanline(os::stdin)!) { ...`  (with the `!` after the
> function call that I'm matching on). Seems like this would be at odds with
> (or defeat the purpose of) the rest of the match body, no?

no, match (bufio::scanline(os::stdin)!) is correct in that context - the
! is essentially a second match which aborts on error types and yields
non-error types, so the result type of bufio::scaneline()! is
([]u8 | io::EOF) and it would just abort if it got an io::error
Details
Message ID
<7353441c-c079-4d96-84a3-4d75d85b469c@app.fastmail.com>
In-Reply-To
<CUQX264TY9A0.15PUIDVEUGHQD@monch> (view parent)
DKIM signature
pass
Download raw message
On Sat, Aug 12, 2023, at 6:23 PM, Ember Sawady wrote:
> mind updating my name in your contacts? i think fastmail must've been a
> bit overzealous in remembering stuff during some brief interactions we
> had on hare-dev a bit over a year ago and it's kinda distracting lol

Ah, thanks for the correction. Updated.

> On Sat Aug 12, 2023 at 10:11 PM UTC, John Gabriele wrote:
>> Looking back at the examples I posted, I see that I kept accidentally
>> writing `match (bufio::scanline(os::stdin)!) { ...`  (with the `!` after the
>> function call that I'm matching on). Seems like this would be at odds with
>> (or defeat the purpose of) the rest of the match body, no?
>
> no, match (bufio::scanline(os::stdin)!) is correct in that context - the
> ! is essentially a second match which aborts on error types and yields
> non-error types, so the result type of bufio::scaneline()! is
> ([]u8 | io::EOF) and it would just abort if it got an io::error

Oh, right! I read this in the tutorial but forgot: I see from `haredoc io::EOF`
that `io::EOF` is not an error (it's type is not defined with a prepended `!`).

Cool, thanks,
-- John
Reply to thread Export thread (mbox)