1. type fruit(sequence unknown_fruit)
- Posted by vmars May 20, 2009
- 889 views
- Last edited May 21, 2009
From A Beginners Guide to Euphoria:
type fruit(sequence unknown_fruit)
sequence valid_fruit
valid_fruit = {"bananas", "apples", "oranges", "pears"}
return find(unknown_fruit, valid_fruit)
end type
This is wierd to me.
It would make more sense like this:
fruit_check = "pears"
fruit fruit_check
puts(1, "Program Run Completed\n")
or
fruit_check = "pears"
fruit_check = fruit(fruit_check)
puts(1, "Program Run Completed\n")
Please, Why is it structured like this?
Thanks!
2. Re: type fruit(sequence unknown_fruit)
- Posted by ChrisB (moderator) May 21, 2009
- 845 views
From A Beginners Guide to Euphoria:
type fruit(sequence unknown_fruit)
sequence valid_fruit
valid_fruit = {"bananas", "apples", "oranges", "pears"}
return find(unknown_fruit, valid_fruit)
end type
This defines the fruit type so you can use it later
This is wierd to me.
It would make more sense like this:
fruit_check = "pears"
fruit fruit_check
puts(1, "Program Run Completed\n")
or
fruit_check = "pears"
fruit_check = fruit(fruit_check)
puts(1, "Program Run Completed\n")
Please, Why is it structured like this?
Thanks!
using it later
fruit fruit_check --defining a variable of type fruit if fruit("pears") then --test of pears is a fruit puts(1, "Valid fruit!\n") fruit_check ="pears" --fruit check can now be "pears" else puts(1, "Not a valid fruit!\n") fruit_check = "pears" --fruit_check can't be "pears" - will crash the program --add pears to the list of valid fruits end if
Chris
3. Re: type fruit(sequence unknown_fruit)
- Posted by ChrisB (moderator) May 21, 2009
- 859 views
Hi
I wish I could edit my posts.
Changes pears in what I wrote to grapes, then you can crash the program!
Chris
4. Re: type fruit(sequence unknown_fruit)
- Posted by euphoric (admin) May 21, 2009
- 847 views
I wish I could edit my posts.
Or maybe there could be a forced preview prior to posting.
5. Re: type fruit(sequence unknown_fruit)
- Posted by ChrisB (moderator) May 21, 2009
- 812 views
I wish I could edit my posts.
Or maybe there could be a forced preview prior to posting.
Ouch. In this case, I did, but I'm just too stupid/lazy at times to be bothered to properly check at times. Just like 90% of my programming time is spent adding the trailing )
Chris
6. Re: type fruit(sequence unknown_fruit)
- Posted by euphoric (admin) May 21, 2009
- 817 views
I wish I could edit my posts.
Or maybe there could be a forced preview prior to posting.
Ouch.
That wasn't meant to cause pain! I'd like a forced preview feature for myself!
7. Re: type fruit(sequence unknown_fruit)
- Posted by mattlewis (admin) May 21, 2009
- 854 views
I wish I could edit my posts.
Or maybe there could be a forced preview prior to posting.
Ouch.
That wasn't meant to cause pain! I'd like a forced preview feature for myself!
I alwyas perveiw.
Matt
8. Re: type fruit(sequence unknown_fruit)
- Posted by vmars May 21, 2009
- 834 views
Do you mean like this?:
fruit fruit_check --defining a variable of type fruit if fruit("grapes") then --test of grapes is a fruit puts(1, "Valid fruit!\n") fruit_check ="grapes" --fruit check can now be "grapes" else puts(1, "Not a valid fruit, so added to list!\n") fruit_check = "grapes" --fruit_check can't be "pears" - will crash the program --add grapes to the list of valid fruits end if
fruit_check = "grapes" fruit_check can't be "pears" - will crash the program
Why, because "pears" is already there?
If so, What if I wanted two "pears" entries?
Thanks!
9. Re: type fruit(sequence unknown_fruit)
- Posted by ChrisB (moderator) May 22, 2009
- 838 views
Do you mean like this?:
fruit fruit_check --defining a variable of type fruit if fruit("grapes") then --test of grapes is a fruit puts(1, "Valid fruit!\n") fruit_check ="grapes" --fruit check can now be "grapes" else puts(1, "Not a valid fruit, so added to list!\n") fruit_check = "grapes" --fruit_check can't be "pears" - will crash the program --add grapes to the list of valid fruits end if
fruit_check = "grapes" fruit_check can't be "pears" - will crash the program
Why, because "pears" is already there?
If so, What if I wanted two "pears" entries?
Thanks!
Hi
Yes, thats what I meant. Of you could have as many pears as you wanted, but the find would return true no matter how many pears you had. Of course, that would always be beaten by a full house (sorry, couldn't resist)
The program was meant to be demonstration of checking the type - you can't add the wrong type to a typed variable, but you can use the type as a function to check if it is the right type.
Sorry I botched it up.
As far as I know, a type is defined at run time, so you can't add another element or range to a type after it is defined. However you could create a function that checks for validity then adds it to a list of valid elements.
Chris